React Native Pressable Example
When “React Native Pressable Example” moves through design, development and browser testing, workplace culture can clarify hand-offs and time spent on revisions while leaving code quality, accessibility and released behaviour as the real measures of success.
For API changes, browser support and current usage patterns, compare the snippet with the React Native project before adopting it in production.
Hi Dev,
In this blog, I will learn you how to use pressable in react native. You can easily use pressable click event in react native. First i will create import namespace pressable from react-native, after I will using pressable using for pressable tag add in react native example.
Pressable uses React Native's Pressability API. For more information around the state machine flow of Pressability and how it works, check out the implementation for Pressability.
Here, I will give you full example for simply display pressable using react native as bellow.
So let's start following example.
Step 1 - Create project
In the first step Run the following command for create project.
expo init PressableExample
Step 2 - App.js
In this step, You will open App.js file and put the code.
import React, { useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
const App = () => {
const [timesPressed, setTimesPressed] = useState(0);
let textLog = '';
if (timesPressed > 1) {
textLog = timesPressed + 'x onPress';
} else if (timesPressed > 0) {
textLog = 'onPress';
}
return (
<View style={styles.container}>
<Pressable
onPress={() => {
setTimesPressed((current) => current + 1);
}}
style={({ pressed }) => [
{
backgroundColor: pressed
? 'rgb(210, 230, 255)'
: 'white'
},
styles.wrapperCustom
]}>
{({ pressed }) => (
<Text style={styles.text}>
{pressed ? 'Pressed!' : 'Press Me'}
</Text>
)}
</Pressable>
<View style={styles.logBox}>
<Text testID="pressable_press_console">{textLog}</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
},
text: {
fontSize: 16
},
wrapperCustom: {
borderRadius: 8,
padding: 6
},
logBox: {
padding: 20,
margin: 10,
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#f0f0f0',
backgroundColor: '#f9f9f9'
}
});
export default App;
Step 3 - Run project
In the last step run your project using bellow command.
expo start
Output
I hope it will help you...
- React Native Material Appbar With Content Component Example
- React Native Refresh Control Tutorial Example
- React Native Material Appbar Example
- React Native Range Slider Example
- React Native Toast Notification Example
- React Native Material Icon List Example
- React Native Layout Props Tutorial
- TextInput Example Using React Native