Lesson 2 of 9
Reanimated 3 - UI-Thread Worklets
React Native Reanimated 3 runs animation logic on the UI thread using Worklets - a subset of JavaScript that executes in C++. This means your animation logic runs at 120fps on ProMotion displays, never drops frames, and doesn't compete with React's render cycle.
Installation
npx expo install react-native-reanimated
# Add plugin to babel.config.js:
# plugins: ['react-native-reanimated/plugin']Core Reanimated Hooks
import Animated, {
useSharedValue,
useAnimatedStyle,
withTiming,
withSpring,
withRepeat,
withSequence,
interpolate,
Extrapolation,
runOnJS,
useDerivedValue,
useAnimatedScrollHandler,
} from 'react-native-reanimated';
// ---- useSharedValue: animation state that lives on UI thread ----
function ToggleCard() {
const expanded = useSharedValue(false);
const height = useSharedValue(60);
// useAnimatedStyle: derives styles from shared values (runs on UI thread)
const animatedStyle = useAnimatedStyle(() => {
'worklet'; // explicit worklet declaration
return {
height: withSpring(expanded.value ? 200 : 60, { damping: 15, stiffness: 100 }),
opacity: withTiming(expanded.value ? 1 : 0.7, { duration: 200 }),
};
});
function toggle() {
expanded.value = !expanded.value; // direct assignment = instant, no batch
}
return (
<Pressable onPress={toggle}>
<Animated.View style={[{ backgroundColor: '#1e293b', borderRadius: 12, overflow: 'hidden' }, animatedStyle]}>
<Text style={{ color: '#f1f5f9', padding: 16, fontWeight: '700' }}>Tap to expand</Text>
</Animated.View>
</Pressable>
);
}
// ---- Scroll-driven animation: parallax header ----
function ParallaxHeader({ headerHeight = 240 }: { headerHeight?: number }) {
const scrollY = useSharedValue(0);
const scrollHandler = useAnimatedScrollHandler(event => {
scrollY.value = event.contentOffset.y;
});
const headerStyle = useAnimatedStyle(() => {
'worklet';
return {
transform: [
{
// Parallax: header moves at half scroll speed
translateY: interpolate(scrollY.value, [0, headerHeight], [0, -headerHeight / 2], Extrapolation.CLAMP),
},
{
// Zoom out as user scrolls up (pulls to zoom in)
scale: interpolate(scrollY.value, [-100, 0], [1.3, 1], Extrapolation.CLAMP),
},
],
opacity: interpolate(scrollY.value, [0, headerHeight * 0.8], [1, 0], Extrapolation.CLAMP),
};
});
const titleStyle = useAnimatedStyle(() => {
'worklet';
return {
opacity: interpolate(scrollY.value, [headerHeight * 0.5, headerHeight], [0, 1], Extrapolation.CLAMP),
};
});
return (
<View style={{ flex: 1 }}>
{/* Sticky title (appears as header fades out) */}
<Animated.View style={[{ position: 'absolute', top: 48, left: 0, right: 0, zIndex: 10, alignItems: 'center' }, titleStyle]}>
<Text style={{ color: '#f1f5f9', fontWeight: '700', fontSize: 16 }}>Parallax Demo</Text>
</Animated.View>
<Animated.ScrollView onScroll={scrollHandler} scrollEventThrottle={16}>
{/* Parallax image */}
<Animated.View style={[{ height: headerHeight, backgroundColor: '#229c6b' }, headerStyle]} />
{/* Content */}
<View style={{ padding: 16 }}>
{Array.from({ length: 30 }, (_, i) => (
<View key={i} style={{ height: 60, backgroundColor: '#1e293b', borderRadius: 10, marginBottom: 8 }} />
))}
</View>
</Animated.ScrollView>
</View>
);
}runOnJS - Calling React Callbacks from Worklets
import { runOnJS } from 'react-native-reanimated';
// Worklets cannot call React state setters directly (they run on UI thread)
// runOnJS schedules the call on the JS thread
function SwipeableCard({ onSwipedAway }: { onSwipedAway: () => void }) {
const translateX = useSharedValue(0);
const opacity = useSharedValue(1);
const SWIPE_THRESHOLD = 150;
// This runs on UI thread:
const gesture = Gesture.Pan()
.onUpdate(event => {
translateX.value = event.translationX;
opacity.value = interpolate(
Math.abs(event.translationX),
[0, SWIPE_THRESHOLD],
[1, 0.4],
Extrapolation.CLAMP
);
})
.onEnd(event => {
if (Math.abs(event.translationX) > SWIPE_THRESHOLD) {
translateX.value = withTiming(event.translationX > 0 ? 400 : -400, { duration: 200 });
opacity.value = withTiming(0, { duration: 200 });
// Call React callback from UI thread:
runOnJS(onSwipedAway)();
} else {
translateX.value = withSpring(0);
opacity.value = withTiming(1);
}
});
const animatedStyle = useAnimatedStyle(() => ({
transform: [{ translateX: translateX.value }],
opacity: opacity.value,
}));
return (
<GestureDetector gesture={gesture}>
<Animated.View style={[{ backgroundColor: '#1e293b', borderRadius: 14, height: 100, padding: 16 }, animatedStyle]}>
<Text style={{ color: '#f1f5f9', fontWeight: '600' }}>Swipe me away โ โ</Text>
</Animated.View>
</GestureDetector>
);
}Tip
Tip
Practice Reanimated 3 UIThread Worklets in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Declaring UI-thread worklets, shared values, and physics-based spring animations
Practice Task
Note
Practice Task - (1) Write a working example of Reanimated 3 UIThread Worklets from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Reanimated 3 UIThread Worklets is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?