How to use pressable in React Native
React Native is an open-source JavaScript framework built on top of React. As React is mainly developed to build web applications, it is primarily designed to build natively rendering mobile applications for iOS and Android.
The pressable component
React Native provides a component named Pressable component that allows us to handle touch events on various UI elements. It helps us detect simple taps or more complex interactions like long presses.
Using the pressable component
Some of the essential Pressable component include:
onPressIn: It is called when the press is activated.onPressOut: It is called when the press is deactivated.onLongPress: It is called when the user presses the finger longer than 500 milliseconds.onPress: It is called after theonPressOut.delayLongPress: It is used to specify the delay (in milliseconds) before theonLongPressis triggered when the user presses down on thepressablecomponent. By default, this value is set to 500 milliseconds.disabled: When its value is set totrueit disables the press functionality. By default, its value isfalse.
Working of the pressable component
When a user touches the pressable component, it triggers the onPressIn functions which leads to the following two possible scenarios.
If the user removes the touch immediately, the
onPressOutfunction is triggered, which is followed by theonPressfunction.
If the user maintains the touch for longer than 500 milliseconds, the
onLongPressfunction is triggered. TheonPressOutfunction is then called when the user releases the touch.
We can handle these events by providing the respective event handlers.
Implementing the Pressable component
Let's use the Pressable component to build an interactive UI. We will create an interface with a pressable button and text boxes showing the touch events and total count of presses on each button press.
import React, {useState} from 'react';
import { Pressable, StyleSheet, Text, View} from 'react-native';
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
padding: 100,
},
text: {
fontSize: 17,
},
title: {
fontSize: 17,
paddingLeft: 10,
},
pressableButton: {
width: 200,
height: 50,
borderRadius: 8,
padding: 15,
margin: 10,
borderWidth: 1,
borderColor: '#636363',
backgroundColor: '#e3e3e3',
},
textBox: {
padding: 15,
margin: 10,
borderWidth: 1,
borderColor: '#636363',
backgroundColor: '#f9f9f9',
},
});
const PressableDemo = () => {
const [pressCount, setPressCount] = useState(0);
const [message, setMessage] = useState(' ');
let pressLog = 'Pressed 0 time';
if (pressCount == 1) {
pressLog = 'Pressed 1 time';
} else if (pressCount > 1) {
pressLog = 'Pressed ' + pressCount + ' times';
}
return (
<View style={styles.container}>
<View>
<Pressable style={styles.pressableButton}
onPressIn={() => {
setPressCount(current => current + 1);
setMessage('Press in'); }}
onPress={() => {
setMessage('Press'); }}
onPressOut={() => {
setMessage('Press out'); }}
onLongPress={() => {
setMessage('Long Press'); }}
// 600 milliseconds delay in onLongPress
delayLongPress={600}
// Whether press functionality is disbaled
//disabled={true}
>
{({pressed}) => (
<Text style={styles.text}>{pressed ? 'Pressed...' : 'Press me...'}</Text>
)}
</Pressable>
</View>
<View>
<Text style={styles.title}>Touch event: </Text>
<Text style={styles.textBox}>{message}</Text>
</View>
<View>
<Text style={styles.title}>Press count: </Text>
<Text style={styles.textBox}>{pressLog}</Text>
</View>
</View>
);
}
export default PressableDemo;Code explanation
The code above is explained below:
Line 1: We import the
ReactanduseStatecomponent from thereactlibrary.Line 2: We import the
Pressable,StyleSheet,Text, andViewcomponent from thereact-nativelibrary.Lines 4–33: We add styling to the
Pressablecomponent for enhancing the UI experience using theStyleSheetcomponent.Line 35: We create a functional component
PressableDemothat returns JSX.Line 37: We create a
pressCountvariable to track the number of times thePressablecomponent is pressed.Line 38: We create a
messagevariable to display the triggered property.Lines 40–45: We define a
pressLogvariable to display the number of times thePressablecomponent has been pressed.Line 51–74: We create a
Pressablecomponent with the following properties.Lines 52–54: We increase the
pressCountvariable and set themessagevalue to"PressIn"when theonPressInprop is triggered.Lines 56–57: We set the
messagevalue to'Press'when theonPressprop is triggered.Lines 59–60: We set the
messagevalue to'Press out'when theonPressOutprop is triggered.Lines 62–63: We set the
messagevalue to'Long press'when theonLongPressprop is triggered.Line 66: We add the delay of 600 milliseconds before the
onLongPressprop is triggered using thedelayLongPressprop.Line 69: We disabled the press functionality by setting the
disabledprop totrue. Uncomment the line to see the results.
Lines 71–73: We define a function which takes
pressedproperty as an argument and displays text when when aPressablecomponent is pressed.Lines 77–80: We displayed the
messagevalue using thetextcomponent to show which event is currently triggered.Lines 82–85: We displayed the
pressLogvalue using thetextcomponent to show the number of times thepressablecomponent is pressed.Line 92: We export the
PressableDemocomponent as a default export.
The Pressable component is a very useful component to add interactivity in React Native. We can use different available props to handle press events (such as tapping or pressing a button) to effectively respond to user interaction, enhancing the overall user experience of the applications.
Free Resources