Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Pressable and TouchableOpacity

Tags:

With the update react native to version 0.63, new components have appeared. Can someone more experienced explain how the Pressable differs from the TouchableOpacity and when it is better to use them.

like image 823
never_give_up Avatar asked Jul 09 '20 08:07

never_give_up


People also ask

What is the difference between TouchableOpacity and Pressable?

Pressable is all-in-one touchable components Pressable component has all the features of Touchable components. Therefore, we can replace all TouchableOpacity, TouchableHighlight, TouchableNativeFeedback and TouchableWithoutFeedback with a single component.

What is use of Pressable in React Native?

Pressable is a Core Component wrapper that can detect various stages of press interactions on any of its defined children.

Is Pressable a view?

Pressable is a view, and you wrap another view into it and set flex:1. This view (that have container style) fill the parent view (Pressable view).

Why we use TouchableOpacity in React Native?

You may consider using TouchableNativeFeedback on Android to display ink surface reaction ripples that respond to the user's touch. TouchableOpacity can be used to provide feedback by reducing the opacity of the button, allowing the background to be seen through while the user is pressing down.


1 Answers

Pressable was a new introduction to RN 0.63, prior to that,Touchable Opacity was the most common used Component to wrap a component or simliar components.

Both their basic functionalities are same, to make a text/image clickable and user interactive.

But with Pressable you get to access a lot new props like :

HitRect, which is such a cool feature, acc to docs :

Fingers are not the most precise instruments, and it is common for users to accidentally activate the wrong element or miss the activation area. To help, Pressable has an optional HitRect you can use to define how far a touch can register away from the the wrapped element. Presses can start anywhere within a HitRect.

This is clearly a better alternative to what we used for hitslop , here its more precise and you define the area.And it doesnt interfere with the child/other components Z-index too.

So basically you get all the feature of a button, touchableOpacity with cool new props. Do check out thier docs : rn-pressable

Hopeit helps.feel free for doubts

like image 134
Gaurav Roy Avatar answered Oct 03 '22 14:10

Gaurav Roy