┌─────────────────────────────┐ │ Swipe the box left or right │ │ │ │ ┌───────────┐ │ │ │ │ │ │ │ BOX │ │ │ │ │ │ │ └───────────┘ │ │ │ └─────────────────────────────┘
Gesture Handler integration in React Native - Mini App: Build & Ship
import React, { useState } from 'react'; import { View, Text, StyleSheet, Animated } from 'react-native'; import { GestureDetector, Gesture } from 'react-native-gesture-handler'; export default function SwipeableBox() { const [boxColor, setBoxColor] = useState('gray'); // TODO: Define swipe gesture here return ( <View style={styles.container}> <Text style={styles.instructions}>Swipe the box left or right</Text> {/* TODO: Wrap box with GestureDetector and apply gesture */} <Animated.View style={[styles.box, { backgroundColor: boxColor }]} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' }, instructions: { fontSize: 18, marginBottom: 20 }, box: { width: 150, height: 150, borderRadius: 10 } });
import React, { useState, useRef } from 'react'; import { View, Text, StyleSheet, Animated } from 'react-native'; import { GestureDetector, Gesture } from 'react-native-gesture-handler'; export default function SwipeableBox() { const [boxColor, setBoxColor] = useState('gray'); const animatedColor = useRef(new Animated.Value(0)).current; // 0=gray, 1=green, -1=red const colorInterpolation = animatedColor.interpolate({ inputRange: [-1, 0, 1], outputRange: ['red', 'gray', 'green'] }); const swipeGesture = Gesture.Pan() .onEnd(event => { if (event.translationX > 50) { setBoxColor('green'); Animated.timing(animatedColor, { toValue: 1, duration: 300, useNativeDriver: false }).start(); } else if (event.translationX < -50) { setBoxColor('red'); Animated.timing(animatedColor, { toValue: -1, duration: 300, useNativeDriver: false }).start(); } else { setBoxColor('gray'); Animated.timing(animatedColor, { toValue: 0, duration: 300, useNativeDriver: false }).start(); } }); return ( <View style={styles.container}> <Text style={styles.instructions}>Swipe the box left or right</Text> <GestureDetector gesture={swipeGesture}> <Animated.View style={[styles.box, { backgroundColor: colorInterpolation }]} /> </GestureDetector> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' }, instructions: { fontSize: 18, marginBottom: 20 }, box: { width: 150, height: 150, borderRadius: 10 } });
This solution uses react-native-gesture-handler to detect horizontal swipe gestures on the box. We create a Pan gesture that listens for the end of the swipe. If the user swipes right more than 50 pixels, the box color changes to green. If swiped left more than 50 pixels, it changes to red. Otherwise, it resets to gray.
We use React Native's Animated.Value to smoothly interpolate the background color between red, gray, and green. The Animated.timing function animates the color change over 300 milliseconds.
The box is wrapped inside a GestureDetector component that applies the gesture to it. This setup ensures the box responds visually and interactively to user swipes.
┌─────────────────────────────┐ │ Swipe the box left or right │ │ │ │ ┌───────────┐ │ │ │ │ │ │ │ BOX │ │ │ │ │ │ │ └───────────┘ │ │ │ └─────────────────────────────┘
Practice
GestureHandlerRootView?Solution
Step 1: Understand the role of GestureHandlerRootView
This component is required to wrap the app so that gesture detection works properly across all child components.Step 2: Differentiate from other purposes
Styling, performance caching, or navigation are unrelated to GestureHandlerRootView's function.Final Answer:
To enable gesture detection and handling throughout the app -> Option AQuick Check:
GestureHandlerRootView enables gestures [OK]
- Thinking it styles the app
- Confusing it with navigation setup
- Assuming it improves performance
Solution
Step 1: Recall correct import syntax
TapGestureHandler is a named export from 'react-native-gesture-handler', so it must be imported with curly braces.Step 2: Eliminate incorrect imports
Default import without braces is wrong, importing from 'react-native' is incorrect, and importing from a subpath is not standard.Final Answer:
import { TapGestureHandler } from 'react-native-gesture-handler'; -> Option CQuick Check:
Named imports use curly braces [OK]
- Using default import instead of named import
- Importing from 'react-native' instead of gesture-handler
- Trying to import from a subpath
import { TapGestureHandler, GestureHandlerRootView } from 'react-native-gesture-handler';
import React from 'react';
import { View, Text, Alert } from 'react-native';
export default function App() {
const onTap = () => Alert.alert('Box tapped!');
return (
);
}Solution
Step 1: Understand TapGestureHandler usage
The onActivated prop triggers the callback when the tap gesture is recognized.Step 2: Analyze the code behavior
When the blue box is tapped, the onTap function runs, showing an alert with 'Box tapped!'.Final Answer:
An alert saying 'Box tapped!' will appear when the blue box is tapped -> Option AQuick Check:
onActivated triggers alert on tap [OK]
- Confusing onActivated with onPress
- Assuming no effect without extra setup
- Expecting color change without code
import { TapGestureHandler } from 'react-native-gesture-handler';
import React from 'react';
import { View } from 'react-native';
export default function App() {
const onTap = () => console.log('Tapped');
return (
);
}Solution
Step 1: Check for GestureHandlerRootView presence
Gesture handlers require the app to be wrapped in GestureHandlerRootView to work properly.Step 2: Evaluate other options
onActivated is correct, View can detect gestures, and size 100x100 is sufficient.Final Answer:
The app is missing the GestureHandlerRootView wrapper -> Option BQuick Check:
Missing GestureHandlerRootView blocks gestures [OK]
- Using onPress instead of onActivated
- Thinking View can't detect gestures
- Assuming size is too small
Solution
Step 1: Understand gesture handler nesting
To detect multiple gestures on the same view, nest handlers so gestures can be recognized distinctly.Step 2: Evaluate options
Using only TapGestureHandler can't detect long press, siblings won't work well on same view, PanGestureHandler is for drag gestures.Final Answer:
Nest LongPressGestureHandler inside TapGestureHandler and handle callbacks separately -> Option DQuick Check:
Nesting handlers enables multiple gesture detection [OK]
- Trying to detect long press with TapGestureHandler only
- Using sibling handlers on same view without nesting
- Confusing PanGestureHandler with tap/long press
Ready to prove it?
Pass the test and this pattern moves unknown
