最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the difference between Pressable and TouchableOpacity? - Stack Overflow

programmeradmin11浏览0评论

With the new React Native version, 0.63, a new ponent called Pressable has been added. How does Pressable differ from the existing TouchableOpacity, and when is it better to use one versus the other?

With the new React Native version, 0.63, a new ponent called Pressable has been added. How does Pressable differ from the existing TouchableOpacity, and when is it better to use one versus the other?

Share Improve this question edited Feb 13 at 21:55 shim 10.1k13 gold badges76 silver badges116 bronze badges asked Jul 9, 2020 at 8:27 never_give_upnever_give_up 8421 gold badge7 silver badges11 bronze badges 3
  • You can read the Pressable Documentation to get a clear Idea. – TripleM Commented Jul 9, 2020 at 11:09
  • Blog post with the announcement: reactnative.dev/blog/2020/07/06/version-0.63#pressable Note that it says "We expect that most people will build and share ponents utilizing Pressable under the hood instead of relying on the default experience of something like TouchableOpacity." – Albert Vila Calvo Commented Feb 18, 2021 at 10:36
  • 1 One thing that I do want to point out for future readers of this post - there is a slight timing difference between the Pressable ponent and TouchableOpacity. In a project that I'm working on that is very timing sensitive, Pressable has slightly more delay between the onPressIn and onPressOut events than TouchableOpacity. It's not enough to cause an issue in most cases, but if you do need something that is more performant, it is worthwhile to do some testing between the two yourself. – Jon Commented Nov 30, 2021 at 0:30
Add a ment  | 

3 Answers 3

Reset to default 64

Pressable was a new introduction to RN 0.63; prior to that, TouchableOpacity was the most monly used ponent to wrap a <Text> ponent or similar ponents.

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), according to docs:

Fingers are not the most precise instruments, and it is mon 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 it's more precise and you define the area. And it doesn't interfere with the child/other ponents z-index too.

So basically you get many of the features of a button, touchableOpacity, with cool new props. Check out the React Native Pressable docs.

NOTE: Also as other ments in this thread suggests, Pressable still doesn't have an animation attached with the onPress Event

Just wanted to add a note about one drawback of Pressable and a workaround.

The drawback is Pressable has no automatic feedback like its TouchableOpacity counterpart.

However, you can add your own custom feedback with Pressable's style prop, which es with a pressed state identifier:

<Pressable
  style={({ pressed }) => [
    { opacity: pressed ? 0.5 : 1.0 }
  ]}
  onPress={() => console.log('Pressed')}
>
 <View><Text>Press Me</Text></View>
</Pressable>

Here is a picture to clear your doubt.

How it works On an element wrapped by

Pressable

:

onPressIn is called when a press is activated. onPressOut is called when the press gesture is deactivated.

After pressing onPressIn, one of two things will happen:

  1. The person will remove their finger, triggering onPressOut followed by onPress.
  2. If the person leaves their finger longer than 370 milliseconds before removing it, onLongPress is triggered. (onPressOut will still fire when they remove their finger.)

Please refer to documentation for more details.

发布评论

评论列表(0)

  1. 暂无评论