Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use TouchableNativeFeedback, TouchableHighlight or TouchableOpacity?

Tags:

react-native

In React Native, there are at least three ways to make a button: TouchableNativeFeedback, TouchableHighlight and TouchableOpacity. There is also TouchableWithoutFeedback, which the documentation clearly states you should not use because "all the elements that respond to press should have a visual feedback when touched".

  • TouchableNativeFeedback is Android only and "replaces the View with another instance of RCTView"
  • TouchableHighlight "adds a view to the view hierarchy"
  • TouchableOpacity works "without changing the view hierarchy"

Are there any other significant differences between the three? Is one of them the goto component? In what case should you use TouchableHighlight over TouchableOpacity? Are there any performance implications?

I am writing an application right now, and find that all three have a significant delay between tap and the action (in this case a navigation change). Is there any way to make it snappier?

like image 403
damusnet Avatar asked Aug 24 '16 12:08

damusnet


People also ask

What does TouchableHighlight do and when do you use it?

TouchableHighlight is a component that is used to provide a wrapper to Views in order to make them respond correctly to touch-based input. On press down the TouchableHighlight component has its opacity decreased which allows the underlying View or other component's style to get highlighted.

What is TouchableNativeFeedback?

A wrapper for making views respond properly to touches (Android only). On Android this component uses native state drawable to display touch feedback.

Can you style TouchableOpacity?

This component fades out when pressed, and fades back in when released. We can style it however we want, just like a View . We can configure the pressed opacity with the activeOpacity prop.

What component do we use to respond properly for touches in react native?

Touchables​ If the basic button doesn't look right for your app, you can build your own button using any of the "Touchable" components provided by React Native. The "Touchable" components provide the capability to capture tapping gestures, and can display feedback when a gesture is recognized.


2 Answers

source: https://medium.com/differential/better-cross-platform-react-native-components-cb8aadeba472, by Nick Wientge

TouchableHighlight

• What it does: Darkens or lightens the background of the element when pressed.

• When to use it: On iOS for touchable elements or buttons that have a solid shape or background, and on ListView items.

TouchableOpacity

• What it does: Lightens the opacity of the entire element when pressed.

• When to use it: On iOS for touchable elements that are standalone text or icons with no background color.

TouchableNativeFeedback

• What it does: Adds a ripple effect to the background when pressed.

• When to use it: On Android for almost all touchable elements.

like image 196
Edan Chetrit Avatar answered Oct 22 '22 05:10

Edan Chetrit


Well, This is how I typically decide what to use:

  • If I'm building for Android-only, and the component is large enough that the native feedback will be visibly different than using the others then I use TouchableNativeFeedback
  • If I want to control the opacity on the component or I want the button to have color when touched, and I don't want to control the focused state of some element inside the Touchable, then I use TouchableHighlight. (TouchableOpacity has got some weird parts when you control opacity yourself).
  • In all other cases, I use TouchableOpacity because it's more "bare" than TouchableHighlight
like image 25
mienaikoe Avatar answered Oct 22 '22 03:10

mienaikoe