​Detecting a left swipe gesture in React Native can be achieved through various methods, each tailored to different use cases and preferences.
A straightforward method involves creating a custom hook that tracks touch positions to determine swipe direction. Here’s how you can implement it:
import { Dimensions } from 'react-native'; const windowWidth = Dimensions.get('window').width; export function useSwipe(onSwipeLeft ?:()=>void, onSwipeRight?:()=>void, rangeOffset = 4) { let firstTouch = 0; // Set user touch start position function onTouchStart(e) { firstTouch = e.nativeEvent.pageX; } // When touch ends, check for swipe directions function onTouchEnd(e) { const positionX = e.nativeEvent.pageX; const range = windowWidth / rangeOffset; if (positionX - firstTouch > range) { onSwipeRight && onSwipeRight(); } else if (firstTouch - positionX > range) { onSwipeLeft && onSwipeLeft(); } } return { onTouchStart, onTouchEnd }; }
In your component, you can utilize this hook as follows:
export default function App() { const { onTouchStart, onTouchEnd } = useSwipe( () => console.log('Swiped left'), () => console.log('Swiped right') ); return (); } Open up App.tsx to start working on your app!
This approach offers a clean implementation without relying on external modules.
a custom hook to detect left and right swipe gestures within a ScrollView that has horizontal={true}. Since a horizontal ScrollView arranges its children in a row direction (flexDirection: ‘row’) and onSwipeLeft and onSwipeRight can detect swipe left/right you can add your logic as you want
React Native’s PanResponder API can be employed to recognize swipe gestures. By tracking the gesture’s movement, you can determine the swipe direction. For instance, by monitoring the dx (change in x-axis position), you can detect left or right swipes.
import React, { useRef } from 'react'; import { View, Text, PanResponder, StyleSheet } from 'react-native'; export default function SwipeDetector() { const panResponder = useRef( PanResponder.create({ onStartShouldSetPanResponder: () => true, onMoveShouldSetPanResponder: () => true, onPanResponderRelease: (evt, gestureState) => { const { dx } = gestureState; __DEV__ && console.log('dx', dx); if (dx > 50) { console.log('Swiped Right'); } else if (dx < -50) { console.log('Swiped Left'); } }, }) ).current; return (); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, text: { fontSize: 20, fontWeight: 'bold', }, }); Swipe Left or Right
This is a lightweight, dependency-free approach to detecting swipe gestures in React Native.
For more advanced gesture recognition, the react-native-gesture-handler library provides components like Swipeable and PanGestureHandler. These components offer enhanced performance and flexibility in handling gestures. For example, Swipeable can be used to create swipeable rows with customizable actions.
Here you need to install react-native-gesture-handler lib from npm
Here is the code:
import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; import { GestureHandlerRootView, HandlerStateChangeEvent, PanGestureHandler, PanGestureHandlerEventPayload, State } from 'react-native-gesture-handler'; export default function SwipeGesture() { const onGestureEvent = (event: HandlerStateChangeEvent) => { const { translationX, state } = event.nativeEvent; if (state === State.END) { if (translationX < -50) { console.log('Swiped Left'); } else if (translationX > 50) { console.log('Swiped Right'); } } }; return ( ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, text: { fontSize: 20, fontWeight: 'bold', }, }); Swipe Left or Right
Another method involves using the onResponderMove event to track touch movements and determine swipe direction. By setting up a responder and monitoring the touch’s movement along the x-axis, you can identify left and right swipes.​
Here is the code
import React, { useRef } from 'react'; import { View, Text, StyleSheet, GestureResponderEvent } from 'react-native'; export default function SwipeResponder() { const startX = useRef(0); const onStartShouldSetResponder = () => true; const onResponderGrant = (event:GestureResponderEvent) => { startX.current = event.nativeEvent.pageX; }; const onResponderRelease = (event:GestureResponderEvent) => { const endX = event.nativeEvent.pageX; const diffX = endX - startX.current; if (diffX > 50) { console.log('Swiped Right'); } else if (diffX < -50) { console.log('Swiped Left'); } }; return (); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, text: { fontSize: 20, fontWeight: 'bold', }, }); Swipe Left or Right 2
This approach is simple, lightweight, and doesn’t require extra dependencies.
The react-native-swipe-gestures library simplifies the process of detecting swipe gestures by providing a GestureRecognizer component. This component allows you to specify handlers for different swipe directions, including left swipes
Here you need to install react-native-swipe-gestures from npm
Here is code
import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; import GestureRecognizer from 'react-native-swipe-gestures'; export default function SwipeGestureComponent() { const onSwipeLeft = () => { console.log('Swiped Left'); }; const onSwipeRight = () => { console.log('Swiped Right'); }; const swipeConfig = { velocityThreshold: 0.3, directionalOffsetThreshold: 80, }; return (); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0', }, text: { fontSize: 20, fontWeight: 'bold', }, }); Swipe Left or Right
This method is quick, easy, and requires minimal setup.
Each of these methods offers a viable solution for detecting left swipe gestures in React Native. The choice of approach depends on your project’s specific requirements and whether you prefer to use external libraries or stick with built-in APIs.
The first four solutions are supported across all React Native frameworks, including Expo Go. However, the last solution (using react-native-swipe-gestures) works only in bare React Native projects and is not compatible with Expo Go.
Work with our skilled React Native developers to accelerate your project and boost its performance.
Hire React Native Developers