In this blog, we are going to learn about the new component in React Native: “Pressable.” We can think of it as an updated version of TouchableOpacity.
Like TouchableOpacity, Pressable is a component wrapper, meaning you need to define a child component within it. Pressable can detect various stages of pressing events.
Pressable provides more functions to handle click events, such as onPressIn, onPressOut, onPress, and onLongPress. I have used all the mentioned functions in the snippet below.
Implementation
Now we are implementing the Pressable Component in React-native Page, step by step.
StatusBar: Sets the status bar style to dark content.
View: View is one of the basic components in the react native and in this for styles, a container that centers its children both vertically and horizontally.
Pressable: Wrapped around a Text component, it handles various press events.
onPress: Calls onPressFunction when the button is pressed.
style: Changes background color based on the pressed state (uses Colors.light or Colors.dark).
onLongPress, onPressIn, onPressOut: Attaches the respective handlers to the Pressable.
Inside the Pressable, it conditionally renders the Text:
If pressed, it shows “Pressed!” with pressedTextStyle.
Otherwise, it shows “Press Me” with unPressedTextStyle.
flex:1,// Use flex to allow the container to fill the screen
justifyContent:'center',
alignItems:'center',
},
unPressedTextStyle:{
padding:10,
color:Colors.white,
},
pressedTextStyle:{
padding:10,
color:Colors.black,
},
pressableStyle:{
borderRadius:5,
shadowColor:'#000',
shadowOffset:{width:0,height:2},
shadowOpacity:0.5,
shadowRadius:2,
elevation:5,
},
});
export defaultApp;
Explain Code
Powered By
Here Is The Output Of The Project:
Pressable Component in React-native
Creates a simple React Native application with a single button that responds to various press events, displaying different messages based on user interactions.
I hope it will help you to integrate the pressable component.
In this blog, we explored the new Pressable component in React Native, which serves as an enhanced alternative to TouchableOpacity.
By utilizing Pressable, we can easily manage various touch interactions, such as onPressIn, onPressOut, onPress, and onLongPress, allowing for a more responsive and engaging user experience.
The provided code snippet demonstrates how to implement these features effectively, showcasing the versatility of Pressable in handling user input.
I hope this blog has helped you understand how to effectively use the Pressable component in React Native and leverage its capabilities to create interactive UIs.
Aniket Raj, a Software Engineer, specializes in eCommerce mobile app development with expertise in Headless PWA. His innovative approach enhances app performance, delivering seamless and scalable solutions for modern eCommerce platforms.
Be the first to comment.