Gestures: keep up with the finger
A finger on glass expects the screen to follow it, every frame. Watch one card take a tap, a double tap, a swipe and a pinch, then see what happens to a drag when the JS thread is busy, and why where the work runs matters more than which library you use.
What you'll take away
- Explain why a single tap feels late when the same card also takes a double tap
- Let pinch and rotate run together with
useSimultaneousGestures, not block each other - Spot why a PanResponder drag freezes while JS is busy, then jumps to the finger
- Keep a drag on the UI thread with gesture-handler and Reanimated, JS only at the end
The 1-minute version
Tap a point and the picture below plays that step.
Press the busy button, then drag the card quickly. Try all three and see which one sticks (drag with a mouse or a finger)
Frames the card stood still mid-drag: 0
You drag a card with PanResponder, and it freezes while the app renders a long list. Why?
The picture: a big phone standing in the night city with one card on its screen, drawn as its user sees it; a circle is the finger. Up the street, the UI thread's hall takes the touches, and gesture-handler recognises them there; behind it stands the JS thread's hall, a bridge between the two. Touches and moves travel as lights, slower than in life so you can follow them, and the drag steps play at about a third of their speed. Red dots at the JS hall's door are moves a busy JS hasn't read yet. A model at 60 frames a second, not a measurement of a real phone.
Compare them all: Gestures on a phone
The same finger drags the card round the same loop in every tile, and the JS thread is busy for the same second (a heavy render). See whose card still follows.
Use it when Simple cases with nothing heavy on JS during the drag
Skip it when Drags that must stay smooth while a list renders or a response arrives
A model: 60 frames a second; a frame whose work sits on a busy thread doesn't happen. Not measured on a phone. Press any tile to see it large above, with when it fits.
What runs where
| Piece | Thread | While JS is busy |
|---|---|---|
| PanResponder, setState | JS | waits |
| gesture-handler recognising touches | UI (native) | keeps going |
| its callbacks with Reanimated (worklets) | UI | keeps going |
| its callbacks with runOnJS: true | JS | waits |
| scheduleOnRN(fn) from a worklet | JS, later | queues |
Combining gestures
gesture-handler 3 builds each gesture with a hook (usePanGesture, useTapGesture, usePinchGesture…) and combines them: useSimultaneousGestures lets them run together (pinch and rotate), useExclusiveGestures gives the first one its chance before the next (a double tap before a tap). Version 2's Gesture.Pan() builder is still exported, with its types renamed Legacy.
In real code
// A card that follows the finger on the UI thread and flies off when swiped: gesture-handler 3 recognises the pan
// natively, its callbacks are worklets (they run on the UI thread), and Reanimated moves the card there too.
// Only the end of the swipe goes back to the JS thread, once.
import { StyleSheet, Text } from "react-native";
import { GestureDetector, GestureHandlerRootView, usePanGesture } from "react-native-gesture-handler";
import Animated, { useAnimatedStyle, useSharedValue, withSpring, withTiming } from "react-native-reanimated";
import { scheduleOnRN } from "react-native-worklets";
const FLY_SPEED = 800; // points per second
const FLY_DISTANCE = 120;
export function SwipeCard({ title, onSwiped }: { title: string; onSwiped: (direction: "left" | "right") => void }) {
const x = useSharedValue(0);
const pan = usePanGesture({
onUpdate: (e) => {
"worklet";
x.value = e.translationX; // every frame, on the UI thread: no trip to JS
},
onDeactivate: (e) => {
"worklet";
const fast = Math.abs(e.velocityX) > FLY_SPEED, far = Math.abs(e.translationX) > FLY_DISTANCE;
if (!fast && !far) { x.value = withSpring(0); return; } // not enough: spring back
const direction = e.translationX > 0 ? "right" : "left";
x.value = withTiming(direction === "right" ? 600 : -600, { duration: 220 });
scheduleOnRN(onSwiped, direction); // one job back on the JS thread, e.g. remove it from state
},
});
const style = useAnimatedStyle(() => ({ transform: [{ translateX: x.value }, { rotate: `${x.value / 20}deg` }] }));
return (
<GestureDetector gesture={pan}>
<Animated.View style={[styles.card, style]}><Text>{title}</Text></Animated.View>
</GestureDetector>
);
}
// gesture-handler needs its root view once, around the app.
export default function App() {
return (
<GestureHandlerRootView style={styles.root}>
<SwipeCard title="Mina, 3 km away" onSwiped={(d) => console.log("swiped", d)} />
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
root: { flex: 1, alignItems: "center", justifyContent: "center" },
card: { width: 260, height: 160, borderRadius: 16, padding: 16, backgroundColor: "#eef0f3" },
});
tsc against each library's own published types (react-native and the libraries this file imports); type-checked only, never run on a phoneIt printed: tsc 6.0.3 (strict): no errors against react-native 0.88.1, react-native-gesture-handler 3.5.0, react-native-reanimated 4.8.0, react-native-worklets 0.14.0 (type-checked only)A swipe card with gesture-handler 3's usePanGesture: the callbacks are worklets on the UI thread writing a Reanimated shared value; a fast or long swipe flies off and scheduleOnRN hands one job back to JS. Type-checked with tsc against the types react-native 0.88, react-native-gesture-handler 3.5, react-native-reanimated 4.8 and react-native-worklets publish; no phone ran it.
Common questions
Why does my drag stutter while the app is loading?
If the drag lives on the JS thread (PanResponder, a setState per move), anything that keeps JS busy, like rendering a long list or parsing a big JSON, makes the card wait. Moving the drag to the UI thread fixes it.
Why gesture-handler together with Reanimated?
gesture-handler recognises touches natively, and Reanimated lets the callbacks and the card's style run on the UI thread as worklets. Together, neither recognising the finger nor moving the card waits for JS.
Is gesture-handler alone enough?
Recognition is native, yes, but if the callbacks run on JS (runOnJS: true, or no Reanimated) and move things with setState, they still wait in JS's queue.
Why is a tap slower when there's also a double tap?
They're exclusive and the double tap gets its chance first, so a single tap waits for the second tap's window to pass. If you don't need a double tap, leave it out and taps fire at once.
How do I pinch and rotate at the same time?
Combine pinch and rotation with useSimultaneousGestures. Normally the gesture that starts first blocks the others; simultaneous lets them run together.
How do I update React state from a gesture?
A gesture's callbacks are worklets on the UI thread and can't call setState directly. Use scheduleOnRN to run a function on JS, and do it once at the end, like when a swipe finishes, not every frame.
Is gesture-handler 3 written differently?
Version 3 builds gestures with hooks such as usePanGesture and useTapGesture, combined with useSimultaneousGestures. Version 2's Gesture.Pan() builder is still exported, but its types are now named Legacy.
Next lesson
Event loopLevel: Intermediate
The card froze because JavaScript has one thread; the event loop shows what holds it up and what waits its turn.