What does the Gesture Responder System do in React Native ?
Last Updated :
24 Jul, 2024
Gesture Responder System allows the app to respond to user interactions and manage the entire lifecycle of the gesture. Here the gesture can be as simple as a single touch or as long as dragging on the screen. The apps today are very attractive as they provide us with different ways of interacting with them and such an app can be even developed in React Native using the Feature called Responder System. The simplest attribute in ReactNative that allows users to interact is onPress. But this can only handle the Click event like clicking on a button.
React-Native: A React Framework to build Cross-Platform Apps with Javascript.
Steps to create a React-native project: To understand to create a react-native project in detail visit here or follow the given steps in short :
- Create a react native project using npx react-native init <projectName>
- In the project folder you will find the index.js, Just paste the below-given code samples
- Run npm run android, It will open the emulator that displays output like shown below
Example: Here you will see the working of the onPress event handler on the Button Component.
index.js
JavaScript
import { AppRegistry } from 'react-native';
import { name as appName } from './app.json';
import React, { Component } from 'react';
import { Button, StyleSheet, View, Text } from 'react-native';
class ButtonView extends Component {
onPressButton() {
alert('You Pressed The Button !')
}
render() {
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Text style={{ padding: 10,
fontSize: 42,
textAlign: 'center',
color: "green" }}>
GeeksforGeeks
</Text>
<Text style={{ padding: 10,
fontSize: 20,
textAlign: 'center' }}>
What does the Gesture Responder System
do in React Native ?
</Text>
<Button
onPress={this.onPressButton}
title="Press Me"
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
buttonContainer: {
margin: 20
}
});
AppRegistry.registerComponent(appName, () => ButtonView);
Output:
To handle other gestures React Native provides something called a Gesture Responder System.
Touchables are the ReactNative Built-in APIs that allow you to capture gestures and provide feedback. So there are 3 kinds of Touchables with different responding capabilities:
- TouchableHighlight: Here the background gets darkened for the feedback
- TouchableOpacity: Here the Button itself partially transparent for feedback
- TouchableWithoutFeedback: As the name tells it gives no feedback
Gesture Responder System makes use of these Touchables along with some lifecycle properties to respond to the user gesture, But using touchable is optional and it is just for feedback. The app basically contains Views in a hierarchy. We can make these Views respond using the Responder Lifecycle. There are 2 methods to initiate the response process
View.props.onStartShouldSetResponder: (event) => true/false
This allows you to decide whether the view wants to become the responder of the touch
View.props.onMoveShouldSetResponder: (event) => true/false
This is called when a touch move occurs and works similarly to onStartShouldSetResponder.
The above two methods are called bubbling patterns which means the deepest node in the hierarchy will become the responder when the entire hierarchy returns true. To have a reverse feature we replace it with the following:
View.props.onStartShouldSetResponderCapture: (event) => true/false
View.props.onMoveShouldSetResponderCapture: (event) => true/false
When these methods return true, the either of the following methods triggers:
View.props.onResponderGrant: (event) => {...}
Triggers when the permission for the responder is granted
View.props.onResponderReject: (event) => {...}
Triggers when some other component in the app is currently responding and it cannot release.
On grant the following handlers can be called :
- onResponderMove: The user moves the finger
- onResponderRelease: The user releases the touch
- onResponderTerminateRequest: When some other component wants to respond. Returns true or false
- onResponderTerminate: The component can no longer respond
The event in every method above is the synthetic touch event that contains properties like Id of the touch, X and Y position, target component, etc.
Example 1: In this example, I will maintain two variables X and Y on the screen that shows the position of touch. The values of the X and Y change when you move your touch on the screen and this is done using the Gesture Responder System.
JavaScript
import { AppRegistry } from 'react-native';
import { name as appName } from './app.json';
import React, { Component } from 'react';
import { Button, StyleSheet, View, Text } from 'react-native';
class Tracker extends Component {
constructor(props) {
super(props);
this.state = { x: 0, y: 0 }
}
render() {
return (
<View style={styles.container}
onStartShouldSetResponder={() => {
return true
}}
onResponderMove={(event) => {
this.setState(state => {
state.x = event.nativeEvent.locationX;
state.y = event.nativeEvent.locationY;
return state;
})
}}
>
<View style={styles.buttonContainer}>
<Text style={{ padding: 10,
fontSize: 42,
textAlign: 'center',
color: "green" }}
selectable={false}>
GeeksforGeeks
</Text>
<Text style={{ padding: 10,
fontSize: 20,
textAlign: 'center' }}
selectable={false}>
What does the Gesture Responder System
do in React Native ?
</Text>
<Text style={{ padding: 10,
fontSize: 20,
textAlign: 'center' }}
selectable={false}>x : {this.state.x}
y : {this.state.y}</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
}
});
AppRegistry.registerComponent(appName, () => Tracker);
Output :
Example 2: In this example, I will create a draggable element that we can drag with the above-given lifecycle methods in Gesture Responder System.
JavaScript
import { AppRegistry } from 'react-native';
import { name as appName } from './app.json';
import React, { Component } from 'react';
import { Button, StyleSheet, View, Text } from 'react-native';
class Draggable extends Component {
constructor(props) {
super(props);
this.state = { x: 50, y: 45 }
}
render() {
return (
<View style={styles.container}
onStartShouldSetResponder={() => {
return true
}}
onResponderMove={(event) => {
let x = event.nativeEvent.pageX, y = event.nativeEvent.pageY;
console.log(x, y)
this.setState(state => {
state.x = x;
state.y = y;
return state;
})
}}
>
<View style={styles.buttonContainer}>
<Text style={{ padding: 10,
fontSize: 42,
textAlign: 'center',
color: "green" }}
selectable={false}>
GeeksforGeeks
</Text>
<Text style={{ padding: 10,
fontSize: 20,
textAlign: 'center' }}
selectable={false}>
What does the Gesture Responder System
do in React Native ?
</Text>
</View>
<Text style={{ padding: 10,
fontSize: 20,
color: 'blue',
fontWeight: 'bold',
textAlign: 'center',
position: "absolute",
top: this.state.y,
left: this.state.x }}
selectable={false}>Draggable Text!</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
buttonContainer: {
margin: 20
}
});
AppRegistry.registerComponent(appName, () => Draggable);
Output:
Similar Reads
What are some features of Fast Refresh in React native ?
In this article, we discuss Fast Refresh. How it is used and what its benefits of it.Fast refresh is a React Native element that allows you to get live feedback for changes in your Respond components. By default, Fast Refresh is enabled, and if it is not enabled then you can enable using "Enable Fas
6 min read
What are Refs used for in React Native ?
Refs can be used to access DOM nodes or React Native components created in the render method.We can use Refs in react native with the help of the useRef hook which returns a ref object. The object has a .current property through which the React native components can be accessed. The returned object
5 min read
What are the steps to create first React Native App ?
React Native is an open-source UI software framework created by Meta Platforms, Inc. It is used to develop applications for Android, Android TV, iOS, etc. Weâre always looking for shorter development cycles, quicker time to deployment, and better app performance. And there are so many hybrid mobile
4 min read
What is the use of ScrollView component in React Native ?
In this article, we are going to learn about the uses of the ScrollView Component. So firstly let's know what is ScrollView? The ScrollView Component is an inbuilt react-native component that serves as a generic scrollable container, with the ability to scroll child components and views inside it. I
8 min read
How to implement Swipe to Refresh in React Native ?
In this article, we will look at how we can use the Swipe Down to Refresh feature in React Native. In applications that show real-time data to the users such as crypto values or availability of a food item in a Food Ordering app, users can do a vertical swipe down on the app to manually refresh the
2 min read
What is intro slider in React Native ?
We will walk through the process of creating a beautiful image slider in a React Native application using the react-native-app-intro-slider package. The image slider will serve as an onboarding feature, introducing users to key features of your app.Prerequisites:React NativeNode and NPMApproach:We'l
3 min read
Getting started with React Native
React Native, also known as RN, is a widely used mobile app framework that relies on JavaScript and It enables developers to build natively-rendered apps for both iOS and Android platforms using the same codebase was introduced by Facebook as an open-source project in 2015 and quickly became one of
7 min read
How to Add Start Stop Device Vibration in React Native?
Vibration is a very useful feature in mobile applications to provide feedback or alerts. In this article, we will create a custom button to start and stop vibrations using React Native. Output Preview:Prerequisites:Node.js and npm installedReact Native development environment set upKnowledge of Reac
3 min read
How to add a Touchable Ripple in react-native ?
React Native is a framework developed by Facebook for creating native-style apps for iOS & Android under one common language, JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can now render m
2 min read
What is a bridge in React Native ?
A React Native app comprises two sides as given below.The JavaScript SideThe Native SideThe Native Side should be Java or Kotlin for Android and Swift or Objective-C for iOS.The huge reason for the popularity of React Native is that a bridge can be created between the JavaScript code and Native lang
7 min read