Pan and Swipe Gestures with Gesture Handler
Install React Native Gesture Handler, build a draggable card using PanGestureHandler and useAnimatedGestureHandler, and snap it back to origin on release.
What Is React Native Gesture Handler?
React Native Gesture Handler replaces the built-in touch system with one that runs entirely on the native UI thread. The default React Native responder system processes touch events on the JavaScript thread, introducing latency when JS is busy. Gesture Handler moves recognition and response to native code, enabling the same snappy feel as native iOS and Android apps.
Gesture Handler is the standard companion to Reanimated — they are designed to work together. It provides pan, pinch, rotation, tap, long-press, and fling recognizers that feed data directly into Reanimated worklets.
// Install:
// npx expo install react-native-gesture-handler
// Wrap your app root with GestureHandlerRootView:
import { GestureHandlerRootView } from 'react-native-gesture-handler';
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<YourApp />
</GestureHandlerRootView>
);
}The Gesture API (v2)
Gesture Handler v2 (the modern API) uses the Gesture factory object to create gestures and the GestureDetector component to attach them to UI. This replaces the older component-based API (PanGestureHandler, TapGestureHandler) with a cleaner compositional model.
You create a gesture object (e.g., Gesture.Pan()), configure it with method chaining, and wrap your component in GestureDetector. The gesture callbacks are worklets that run on the UI thread.
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
const panGesture = Gesture.Pan()
.onUpdate((event) => {
'worklet';
// event.translationX, event.translationY available
});
return (
<GestureDetector gesture={panGesture}>
<Animated.View style={animatedStyle} />
</GestureDetector>
);All lessons in this course
- Installing Reanimated and Shared Values
- Worklets and Running Code on the UI Thread
- Pan and Swipe Gestures with Gesture Handler
- Pinch-to-Zoom and Rotation