react-native-swipe-gestures is a React Native component designed to detect and handle 4-directional swipe gestures: up, down, left, and right. It provides a `GestureRecognizer` component that wraps other UI elements, enabling them to respond to these directional swipe events. The library allows for fine-grained control over gesture sensitivity through configurable parameters such as `velocityThreshold`, `directionalOffsetThreshold`, and `gestureIsClickThreshold`. Currently at version 1.0.5, this package offers a straightforward, declarative API for implementing common swipe interactions, abstracting the complexities of React Native's underlying `PanResponder`. It also ships with TypeScript type definitions, providing an improved development experience for TypeScript users. While effective for basic swipe detection, for more complex gesture interactions or higher performance requirements (e.g., gestures running on the UI thread), alternative libraries like `react-native-gesture-handler` (v2.0+) might be considered.
npm install react-native-swipe-gesturesVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to integrate `GestureRecognizer` into a React Native component using functional components and hooks. It shows how to define callback functions for each swipe direction and a general `onSwipe` handler to update the UI based on the detected gesture and its configuration. The component changes its background color and text based on the swipe direction.
For new projects or those requiring optimal performance, consider using `react-native-gesture-handler` (especially v2.0+ or v3.0+), which performs gesture recognition on the UI thread for smoother interactions.
Ensure the `GestureRecognizer` component or its parent has appropriate `flex` properties or explicit `width` and `height` to occupy a visible area where gestures can be performed. For example, `style={{flex: 1}}` or specific pixel dimensions.Familiarize yourself with the `gestureState` properties, especially `dx`, `dy`, `vx`, `vy` (change in x/y, velocity in x/y) for debugging or custom logic. The `swipeDirections` constants simplify handling common cases.
Carefully manage the React Native Gesture Responder System's propagation and capture phases. If conflicts arise, consider restructuring your component hierarchy or moving to a more robust gesture library like `react-native-gesture-handler` which offers better composition capabilities.
If using CommonJS, `GestureRecognizer` is the default export: `const GestureRecognizer = require('react-native-swipe-gestures').default;`. Named exports like `swipeDirections` are accessed as `const { swipeDirections } = require('react-native-swipe-gestures');`.Ensure the import statement for `GestureRecognizer` is correct: `import GestureRecognizer from 'react-native-swipe-gestures';`. If using CommonJS, use `const GestureRecognizer = require('react-native-swipe-gestures').default;`.Verify that `GestureRecognizer` or its containing `View` has a defined width, height, or `flex` property (e.g., `style={{flex: 1}}`) so it can capture touch interactions.