React Native theorytheory 0/50 · 0%
Animation · hard

47. Reanimated and worklets

High-performance gesture-driven animations.

`react-native-reanimated` runs animation logic in "worklets" — small JS functions executed on the UI thread — enabling smooth 60fps gesture-driven interactions like a swipeable transaction row, without round-tripping to the JS thread per frame.

import { useSharedValue, useAnimatedStyle, withSpring } from "react-native-reanimated";

function useSwipeAnimation() {
  const translateX = useSharedValue(0);
  const style = useAnimatedStyle(() => ({
    transform: [{ translateX: translateX.value }],
  }));
  function dismiss() {
    translateX.value = withSpring(-400);
  }
  return { style, dismiss };
}

Because worklets run on the UI thread, they can't directly call arbitrary JS functions or access most React state; `runOnJS` is the escape hatch when a worklet needs to trigger JS-side logic, like updating a store after a swipe completes.

Check your understanding

  1. 1. Where do Reanimated worklets execute?

  2. 2. What must you use to call regular JS from a worklet?

  3. 3. Why does UI-thread execution matter for gesture-driven UI?