Introduction to React Native Gesture Handler
React Native Gesture Handler is a powerful library that allows you to create complex and fluid gestures in your React Native applications. It provides a declarative way to handle user interactions, offering more control and better performance compared to the built-in Responder System for complex animations and gestures.
Why Use Gesture Handler?
While React Native's built-in
PanResponder
- Declarative API: Easier to define and manage gesture states and transitions.
- Native Performance: Gestures are processed on the native thread, leading to smoother animations and responsiveness, especially for complex interactions.
- Composability: Gestures can be combined and chained to create sophisticated interactions.
- Interruption Handling: Gracefully handles gesture interruptions and cancellations.
Core Concepts
Gesture Handler provides components to wrap your UI elements and define gesture behaviors.
You'll typically wrap your view components with GestureDetector and attach specific gesture recognizers like PanGestureHandler, TapGestureHandler, etc.
The library introduces several components that act as wrappers around your existing React Native views. The most fundamental is GestureDetector, which takes a gesture prop. This gesture prop is an instance of a gesture recognizer, such as PanGestureHandler, TapGestureHandler, PinchGestureHandler, RotationGestureHandler, and LongPressGestureHandler. These recognizers are configured with callbacks that fire based on the gesture's state changes (e.g., onBegin, onUpdate, onEnd).
Native thread processing for smoother performance and responsiveness.
Common Gesture Handlers
| Handler | Description | Common Use Cases |
|---|---|---|
| PanGestureHandler | Detects drag gestures (swiping). | Dragging elements, scrolling, swiping cards. |
| TapGestureHandler | Detects single or multiple taps. | Button presses, toggling elements. |
| PinchGestureHandler | Detects pinch gestures (scaling). | Zooming images or maps. |
| RotationGestureHandler | Detects rotation gestures. | Rotating objects, adjusting sliders. |
| LongPressGestureHandler | Detects a press held for a duration. | Context menus, long-press actions. |
These handlers can be combined to create multi-touch gestures or sequences of gestures. For instance, you could combine a
PanGestureHandler
PinchGestureHandler
State Management and Animation Integration
Gesture Handler works seamlessly with animation libraries like
react-native-reanimated
useAnimatedGestureHandler
react-native-reanimated
Consider a scenario where you want to create a draggable card that snaps back to its original position. You would use PanGestureHandler to track the card's movement. The onUpdate callback would receive the translation values, which you'd use to update the card's position. On onEnd, you'd trigger an animation to return the card to its initial state if it hasn't been 'dropped' in a new position. This involves mapping gesture translation to animated values.
Text-based content
Library pages focus on text content
Remember to wrap your gesture-enabled components with the GestureHandlerRootView component at the root of your application for Gesture Handler to function correctly.
Getting Started with an Example
Here's a basic example of a draggable square using
PanGestureHandler
react-native-reanimated
import React from 'react';import { StyleSheet } from 'react-native';import Animated from 'react-native-reanimated';import { GestureHandlerRootView, PanGestureHandler } from 'react-native-gesture-handler';const { useSharedValue, useAnimatedStyle, useAnimatedGestureHandler } = Animated;const DraggableSquare = () => {const translateX = useSharedValue(0);const translateY = useSharedValue(0);const panGestureHandler = useAnimatedGestureHandler({onStart: (event, ctx) => {ctx.translateX = translateX.value;ctx.translateY = translateY.value;},onActive: (event, ctx) => {translateX.value = ctx.translateX + event.translationX;translateY.value = ctx.translateY + event.translationY;},onEnd: (event) => {// Optional: Add logic for snapping back or dropping},});const animatedStyle = useAnimatedStyle(() => {return {transform: [{ translateX: translateX.value },{ translateY: translateY.value },],};});return ();};const styles = StyleSheet.create({square: {width: 100,height: 100,backgroundColor: 'tomato',borderRadius: 10,},});export default DraggableSquare;
GestureHandlerRootView?It must wrap the root of your application for Gesture Handler to function correctly.
Learning Resources
The official and most comprehensive resource for understanding and implementing React Native Gesture Handler.
Learn how to seamlessly integrate Gesture Handler with Reanimated for powerful animations.
An introductory blog post from the React Native team explaining the benefits and basic usage of Gesture Handler.
A video tutorial demonstrating how to create complex gestures using the library.
Explore various examples and use cases directly from the Gesture Handler GitHub repository.
A blog post detailing the synergy between Gesture Handler and Reanimated, focusing on gesture states.
Detailed API documentation for all available gesture handlers and their configuration options.
A practical video tutorial showing how to implement swipeable list items.
A comparison article highlighting the differences and advantages of using Gesture Handler.
A structured course module that covers advanced gesture handling techniques.