React Native

How do you handle gestures in a React Native app?

Medium
5
Added
You can handle gestures in a React Native app using the Gesture Handler library, which provides a robust way to handle gestures like taps, swipes, and more.

Solution Code

React Native
-- Example Code --
import React from "react";
import { View, Text } from "react-native";
import { TapGestureHandler, State } from "react-native-gesture-handler";

const App = () => {
  const handlePress = (event) => {
    if (event.nativeEvent.state === State.ACTIVATED) {
      console.log("Button pressed");
    }
  };

  return (
    <TapGestureHandler onHandlerStateChange={handlePress}>
      <View style={{ width: 100, height: 100, backgroundColor: "blue" }}>
        <Text>Tap Me</Text>
      </View>
    </TapGestureHandler>
  );
};

export default App;
Explanation
This code demonstrates how to handle gestures using the Gesture Handler library.

Guided Hints

{'hint': 'Use the Gesture Handler library for gestures.'}
{'hint': 'Handle different gesture states.'}