Start a Project

Pressable Component in React native

Pressable Component in React native

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.

Imports

React: The library for building user interfaces.

Pressable: A component that responds to touch interactions.

StatusBar: Used to control the app’s status bar appearance.

StyleSheet: For creating styles.

Text: To render text on the screen.

ToastAndroid: A module for displaying brief messages on Android.

View: A container for layout and style.

Colors: Provides default colors for the app.

Event Handlers

These are four event handler functions that display toast messages when the corresponding press events occur:

 

JSX Layout

Inside the Pressable, it conditionally renders the Text:

Let’s do some code:

Here Is The Output Of The Project:

I hope it will help you to integrate the pressable component.

References

https://reactnative.dev/docs/pressable,

https://reactnative.dev/docs/getting-started

Conclusion

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.

You can also check other blogs from here.

Thank you for reading! ❤️

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.

Exit mobile version