Hello React Native Friends,
In today’s blog, I will explain about Pressable component in React Native.
React Native provides several components to handle touch interactions, such as TouchableOpacity, TouchableHighlight, and TouchableWithoutFeedback. However, with the introduction of Pressable, developers now have a more versatile and performant way to handle press events.
Pressable is a core component in React Native that allows you to detect various touch interactions, such as press, long press, and press-in/out events. It offers more control and flexibility compared to its predecessors, making it a preferred choice for modern React Native applications.
<Pressable onPress={onPressFunction}> <Text>I'm pressable!</Text> </Pressable>
Pressable comes with a variety of props that allow you to customize its behavior:
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={() => Alert.alert('Button Pressed')} onLongPress={() => Alert.alert('Button Long Pressed')} onPressIn={() => console.log('Press In')} onPressOut={() => console.log('Press Out')} style={({ pressed }) => [ { backgroundColor: pressed ? 'rgb(210, 230, 255)' : 'white' }, styles.button ]} hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} android_ripple={{ color: 'blue', borderless: false }} disabled={false} > <Text style={styles.text}>Press Me</Text> </Pressable> </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;
Pressable is a powerful and flexible component for handling touch interactions in React Native applications. It provides better control, performance, and customization options compared to its predecessors. By incorporating Pressable into your mobile app, you can enhance the user experience and ensure your app remains responsive and intuitive.
Whether you’re building a simple button or a complex gesture-based interface, Pressable is a valuable tool in your React Native toolkit. Hire a React Native Developer to give it a try in your next project and see the difference it makes!
Happy Coding!
A sudden drop in organic traffic can be alarming for any business. Organic traffic is…
In the fast-paced world of eCommerce, effective communication is key to maintaining customer satisfaction and…
In the Remix framework, handling URL parameters is a common task when building dynamic web…
In this blog post, we'll show you how to implement frontend design customization in the…
Starting an eCommerce business with Shopify can be easy yet challenging in many different ways,…
Hello Magento Friends, In Magento 2, you can customize the checkout process to enhance the…