When #13's status changes from "Ironing" to "Ready for pickup", its badge grows for a moment and settles back, and its colour slides from blue to green. That's what we build in this lesson. Under the hood, an animation is just a value that changes a little from frame to frame, 60 times per second on most phones. The question is who computes each of those frames. Mrs. Rini's cashier, busy counting money (the JavaScript thread), could slide the cards on the board by hand as well, but the cards stop every time he's busy counting. Reanimated gives the cashier an assistant (the UI thread) who slides the cards while the cashier keeps counting.
Built-in Animated and Reanimated 4
React Native ships Animated. With useNativeDriver: true, the animation is handed to native and runs on the UI thread, but only for non-layout styles: transform, opacity and colours. For width or height React Native refuses: Style property 'width' is not supported by native animated module. So a size animation has to use useNativeDriver: false, and every frame is computed in JavaScript.
npx expo install react-native-reanimated react-native-worklets # SDK 57: 4.5.1 and 0.10.1
- npm already has Reanimated 4.7.1, but
npx expo installinstalls the version tested for your project's SDK: 4.5.1 on SDK 57, 4.7.1 on SDK 58. Reanimated ships in Expo Go. - Reanimated 4 supports the New Architecture only. React Native 0.86 on SDK 57 is New Architecture only anyway, so you're fine.
- A worklet is a function that runs on the UI thread. Its Babel plugin (
react-native-worklets/plugin) is added automatically bybabel-preset-expo. Old tutorials that tell you to addreact-native-reanimated/plugintobabel.config.jsno longer apply.
Shared values, useAnimatedStyle, withTiming, withSpring
// src/components/StatusBadge.tsx
import { useEffect } from 'react';
import { Text } from 'react-native';
import Animated, { useAnimatedStyle, useSharedValue, withSequence, withSpring, withTiming } from 'react-native-reanimated';
import type { Status } from '@/data/orders';
const COLORS: Record<Status, string> = { Queued: '#64748b', Washing: '#0369a1', Ironing: '#1d4ed8', 'Ready for pickup': '#047857' };
export function StatusBadge({ status }: { status: Status }) {
const scale = useSharedValue(1);
useEffect(() => {
if (status === 'Ready for pickup') {
scale.value = withSequence(
withTiming(1.15, { duration: 120 }),
withSpring(1, { damping: 12, stiffness: 180 }),
);
}
}, [status, scale]);
const scaleStyle = useAnimatedStyle(() => ({ transform: [{ scale: scale.value }] }));
return (
<Animated.View
style={[
{ paddingHorizontal: 10, paddingVertical: 4, borderRadius: 999, backgroundColor: COLORS[status] },
{ transitionProperty: 'backgroundColor', transitionDuration: 300 }, // a Reanimated 4 CSS transition
scaleStyle,
]}>
<Text style={{ color: '#fff', fontWeight: '700' }}>{status}</Text>
</Animated.View>
);
}
useSharedValue(1)creates a value both threads can read. Changingscale.valuedoesn't re-render the component.useAnimatedStyleis a worklet: it's recomputed on the UI thread wheneverscale.valuechanges.withTiming(1.15, { duration: 120 })rises in 120 ms. With no options,withTiminguses 300 ms andEasing.inOut(Easing.quad).withSpring(1, { damping: 12, stiffness: 180 })comes back with a bounce.withSequenceruns both in order.transitionPropertyandtransitionDurationare the CSS-style transitions new in Reanimated 4: wheneverbackgroundColorchanges, the colour slides over 300 ms, no shared value needed.
The numbers, computed with Reanimated 4.5.1's spring formula at 60 frames per second (spring times count from when the spring starts):
| Part | What happens |
|---|---|
withTiming |
1.000 → 1.006 → 1.023 → 1.052 → 1.091 → 1.122 → 1.142 → 1.150 at 120 ms |
withSpring({ damping: 12, stiffness: 180 }) |
dips to 0.927 at about 483 ms, rises to 1.035 at about 967 ms, within 0.005 from about 2,083 ms, done at about 6,400 ms |
withSpring(1) with no options |
never goes below 1, within 0.005 from about 350 ms, done at about 833 ms |
Reanimated 4's default spring (stiffness: 900, damping: 120, mass: 4) is critically damped, so it doesn't bounce. Reanimated 3's used to (damping: 10, stiffness: 100). Want a bounce? Write damping and stiffness yourself. The mass you don't write stays 4, so the table's second row uses mass 4.
Two threads: why Reanimated stays smooth
Guess first: #13's badge is mid-animation, and right in the middle JavaScript is busy for 300 ms reading the freshly refetched order list. With Animated (useNativeDriver: false) and with Reanimated withSpring: which one stays smooth?
JavaScript busy for 300 ms means about 18 frames (300 / 16.7) that JavaScript can't compute.
| Approach | Runs on | While JavaScript is busy for 300 ms |
|---|---|---|
Animated, useNativeDriver: false |
the JS thread, every frame | stands still for about 18 frames: it stutters |
Animated, useNativeDriver: true |
the UI thread | smooth, but only transform, opacity and colours |
Reanimated withTiming / withSpring |
the UI thread (worklet) | smooth, including width and height |
So Reanimated stays smooth, and Animated without the native driver stutters. The animation is only started from JavaScript (in useEffect); after that its frames are computed on the UI thread.
A list that changes: entering, exiting, layout
<Animated.FlatList data={orders} itemLayoutAnimation={LinearTransition} renderItem={({ item }) => (
<Animated.View entering={FadeIn} exiting={FadeOut}><OrderRow order={item} /></Animated.View>
)} />
With the "Ironing" filter, once #13 becomes "Ready for pickup", its row fades out (FadeOut, 300 ms by default) and the other rows slide into place (LinearTransition). Pull to refresh stays refreshing and onRefresh from the API lesson: don't build your own gesture for it.
For other gestures, react-native-gesture-handler gives you GestureDetector and gestures such as tap and pan. In version 3 (3.3 on SDK 58) they are hooks: useTapGesture() and usePanGesture(), with the callbacks onBegin, onActivate, onUpdate, onDeactivate and onFinalize. In version 2 (2.32.0 on SDK 57 and older) they are Gesture.Tap() and Gesture.Pan(), which version 3 deprecates. Their callbacks are worklets too, so touch feedback (a row shrinking to 0.97 while pressed, say) runs on the UI thread. Wrap the root layout in <GestureHandlerRootView style={{ flex: 1 }}>.
Reduce motion
Some people get dizzy from movement, so they turn on "Reduce motion" in the phone's settings. Reanimated's default is ReduceMotion.System: when that setting is on, withTiming, withSpring and layout animations jump straight to the final value. The badge still turns green, just without the movement. Reanimated's useReducedMotion() gives the value at app start. For changes while the app runs, use AccessibilityInfo.isReduceMotionEnabled() and the 'reduceMotionChanged' event.
⚠️ Why does the animation stutter, or not run at all?
useStatefor a per-frame value. Every frame becomes a re-render. UseuseSharedValue.useNativeDriver: trueforwidthorheight. It's refused. Use Reanimated, orscaleXandscaleY.- Adding
react-native-reanimated/pluginto Babel yourself. On SDK 57babel-preset-expoalready adds the worklets plugin. - Expecting
withSpringwith no options to bounce. In Reanimated 4 the default is critically damped. - Gestures without
GestureHandlerRootView. The gestures aren't recognised.
TL;DR: Built-in Animated can run on the UI thread with useNativeDriver: true, but only for transform, opacity and colours; without it every frame is computed in JavaScript and stutters when JavaScript is busy. Reanimated 4 (4.5.1 and react-native-worklets 0.10.1 on SDK 57, New Architecture only, Babel plugin automatic) runs animations as worklets on the UI thread: useSharedValue for the value, useAnimatedStyle for the style, withTiming (300 ms by default) and withSpring (critically damped by default in v4; damping: 12, stiffness: 180 with the default mass of 4 bounces down to 0.927), plus CSS transitions (transitionProperty, transitionDuration: 300) for the badge colour. entering, exiting and itemLayoutAnimation={LinearTransition} animate the list; pull to refresh stays RefreshControl; gestures go through GestureDetector inside GestureHandlerRootView. Respect "Reduce motion": Reanimated jumps to the final value by default.
⚡ An animated value and style
Write:
const scale = useSharedValue(1);
const scaleStyle = useAnimatedStyle(() => ({ transform: [{ scale: scale.value }] }));
Meaning: useSharedValue creates a value both threads can read, and changing scale.value doesn't re-render the component. useAnimatedStyle is a worklet: it's recomputed on the UI thread whenever scale.value changes.
⚡ Grow, then bounce
Write:
scale.value = withSequence(
withTiming(1.15, { duration: 120 }),
withSpring(1, { damping: 12, stiffness: 180 }),
);
Meaning: withTiming rises to 1.15 in 120 ms, then withSpring comes back to 1 with a bounce. Reanimated 4's default spring is critically damped, so it doesn't bounce. Want a bounce? Write damping and stiffness yourself.
⚡ While JavaScript is busy
Guess first: in the middle of badge #13's animation, JavaScript is busy for 300 ms. Animated without the native driver, or Reanimated withSpring: which one stays smooth?
Reanimated. Animated with useNativeDriver: false computes every frame in JavaScript, so it stands still for about 18 frames and stutters.
Meaning: A Reanimated animation is only started from JavaScript, in useEffect. After that its frames are computed on the UI thread.