React Native Swipe Gestures

1.0.5 · maintenance · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import React, { Component, useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import GestureRecognizer, { swipeDirections } from 'react-native-swipe-gestures';

const App = () => {
  const [myText, setMyText] = useState('I\'m ready to get swiped!');
  const [gestureName, setGestureName] = useState('none');
  const [backgroundColor, setBackgroundColor] = useState('#fff');

  const onSwipeUp = () => setMyText('You swiped up!');
  const onSwipeDown = () => setMyText('You swiped down!');
  const onSwipeLeft = () => setMyText('You swiped left!');
  const onSwipeRight = () => setMyText('You swiped right!');

  const onSwipe = (direction) => {
    const { SWIPE_UP, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT } = swipeDirections;
    setGestureName(direction);
    switch (direction) {
      case SWIPE_UP: setBackgroundColor('red'); break;
      case SWIPE_DOWN: setBackgroundColor('green'); break;
      case SWIPE_LEFT: setBackgroundColor('blue'); break;
      case SWIPE_RIGHT: setBackgroundColor('yellow'); break;
      default: setBackgroundColor('#fff'); break;
    }
  };

  const config = {
    velocityThreshold: 0.3,
    directionalOffsetThreshold: 80,
    gestureIsClickThreshold: 5
  };

  return (
    <View style={styles.container}>
      <GestureRecognizer
        onSwipe={(direction, state) => onSwipe(direction, state)}
        onSwipeUp={(state) => onSwipeUp(state)}
        onSwipeDown={(state) => onSwipeDown(state)}
        onSwipeLeft={(state) => onSwipeLeft(state)}
        onSwipeRight={(state) => onSwipeRight(state)}
        config={config}
        style={[styles.gestureArea, { backgroundColor: backgroundColor }]}
      >
        <Text style={styles.text}>{myText}</Text>
        <Text style={styles.text}>onSwipe callback received: {gestureName}</Text>
      </GestureRecognizer>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  gestureArea: {
    flex: 1,
    width: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },
  text: {
    fontSize: 20,
    color: 'black',
    marginBottom: 10,
  },
});

export default App;

view raw JSON →