Consider this React Native code using Gesture Handler:
import { TapGestureHandler } from 'react-native-gesture-handler';
import { View, Text } from 'react-native';
export default function App() {
const onSingleTap = () => alert('Tapped!');
return (
Tap me
);
}What is the expected behavior when the user taps the text?
import { TapGestureHandler } from 'react-native-gesture-handler'; import { View, Text } from 'react-native'; export default function App() { const onSingleTap = () => alert('Tapped!'); return ( <TapGestureHandler onActivated={onSingleTap}> <View style={{ padding: 20, backgroundColor: '#ddd' }}> <Text>Tap me</Text> </View> </TapGestureHandler> ); }
Check the onActivated prop of TapGestureHandler and what it triggers.
The onActivated prop fires when the tap gesture is recognized. Here, it triggers an alert with 'Tapped!'.
In React Native Gesture Handler, the PanGestureHandler has an onGestureEvent prop.
When does this callback get called?
import { PanGestureHandler } from 'react-native-gesture-handler'; import { View } from 'react-native'; export default function App() { const onGestureEvent = event => { console.log('Gesture event:', event.nativeEvent.translationX); }; return ( <PanGestureHandler onGestureEvent={onGestureEvent}> <View style={{ flex: 1, backgroundColor: '#eee' }} /> </PanGestureHandler> ); }
Think about how dragging your finger on the screen updates position continuously.
The onGestureEvent callback fires repeatedly as the user moves their finger, providing continuous updates of the gesture.
To use React Native Gesture Handler properly, the app root must be wrapped with GestureHandlerRootView.
Which option shows the correct import and usage?
Check the import syntax and the style prop on GestureHandlerRootView.
The correct import uses curly braces. The root view must have flex:1 style to fill the screen.
You want to enable swipe back gesture on iOS using React Navigation and Gesture Handler.
Which approach correctly integrates them?
Think about where GestureHandlerRootView should be placed and React Navigation's gesture options.
GestureHandlerRootView must wrap the navigation container. React Navigation's stack navigator supports swipe back with gestureEnabled.
Look at this React Native code snippet:
import { PanGestureHandler } from 'react-native-gesture-handler';
import { View } from 'react-native';
export default function App() {
const onGestureEvent = event => console.log(event.nativeEvent.translationX);
return (
);
}The red square does not respond to pan gestures. What is the most likely cause?
import { PanGestureHandler } from 'react-native-gesture-handler'; import { View } from 'react-native'; export default function App() { const onGestureEvent = event => console.log(event.nativeEvent.translationX); return ( <View style={{ flex: 1 }}> <PanGestureHandler onGestureEvent={onGestureEvent}> <View style={{ width: 100, height: 100, backgroundColor: 'red' }} /> </PanGestureHandler> </View> ); }
Remember the required root wrapper for Gesture Handler to work properly.
GestureHandlerRootView must wrap the app root to enable gesture recognition. Without it, gestures do not work.