A phone draws 60 frames a second, 120 on a ProMotion display. That is 16.7 milliseconds per frame, or 8.3. Any animation driven from JavaScript has to produce each frame inside that budget, and it is sharing the thread with your rendering, your data fetching and everything else. The Animated API that ships with React Native can hand some of this to the native side with useNativeDriver: true, but that only works for transforms and opacity, and never for layout.
Reanimated solves it differently. Animation code is compiled into worklets - small functions that run on the UI thread in their own JavaScript context. A shared value is memory both threads can see. When a worklet writes to a shared value and an animated style reads it, the whole loop happens on the UI thread, and JavaScript can be completely blocked without the animation stuttering.
The rule that follows: inside a worklet, do not call back into JavaScript. If you must - to update React state when an animation finishes - use runOnJS, and understand that you have just scheduled work on the thread you were avoiding.
useAnimatedStyle is where the two meet. withTiming, withSpring and withSequence describe the motion, useAnimatedScrollHandler gives you scroll position without a re-render per pixel, and Layout transitions animate a view moving because its siblings changed.
import Animated, {
useSharedValue, useAnimatedStyle, useAnimatedScrollHandler,
withTiming, withSpring, withSequence, interpolate, Extrapolation, runOnJS,
} from 'react-native-reanimated';
import { useState } from 'react';
import { View, Text, Pressable } from 'react-native';
export function CollapsingHeader() {
const scrollY = useSharedValue(0);
const shake = useSharedValue(0);
const [finished, setFinished] = useState(false);
// Runs on the UI thread. No re-render per scrolled pixel.
const onScroll = useAnimatedScrollHandler((event) => {
scrollY.value = event.contentOffset.y;
});
const headerStyle = useAnimatedStyle(() => ({
height: interpolate(scrollY.value, [0, 120], [200, 64], Extrapolation.CLAMP),
opacity: interpolate(scrollY.value, [0, 90], [1, 0.4], Extrapolation.CLAMP),
}));
const shakeStyle = useAnimatedStyle(() => ({
transform: [{ translateX: shake.value }],
}));
return (
<View style={{ flex: 1 }}>
<Animated.View style={[{ backgroundColor: '#0c7' }, headerStyle]} />
<Animated.ScrollView onScroll={onScroll} scrollEventThrottle={16}>
<Pressable
onPress={() => {
shake.value = withSequence(
withTiming(-10, { duration: 50 }),
withTiming(10, { duration: 50 }),
// The callback runs on the UI thread, so crossing back to
// JavaScript has to be explicit.
withSpring(0, {}, (done) => {
if (done) runOnJS(setFinished)(true);
}),
);
}}
>
<Animated.View style={shakeStyle}>
<Text>Shake me {finished ? '(done)' : ''}</Text>
</Animated.View>
</Pressable>
<View style={{ height: 1200 }} />
</Animated.ScrollView>
</View>
);
}
Open this example in the editor and change it. Nothing you type is saved.