Is there a way in which I can ignore touch events on Text in React Native?

React Native

React Native Problem Overview


I want to implement floating labels, for this I have a Text Component Above a TextInput. I want to ignore all the touch events on the Text so that the TextInput Under it gets all the events. Is there a way I can do this ? In CSS we used to have pointer-events none, I am not sure how to do this in react native.

React Native Solutions


Solution 1 - React Native

In react-native, pointerEvents is a prop, not a style.

    <View pointerEvents="none" />

Solution 2 - React Native

Add pointerEvents: 'none' to the Text component. This allows touch events to go to the ancestors of the component, but not the component itself or its children.

React Native also supports 'box-none', which allows touch events to go to the ancestors and children of the component, and excludes only the component itself.

Solution 3 - React Native

I had the same problem as Cryszon. On Android is seems like pointerEvents="none" doesn't work for Text components.

> Wrapping the Text in a View and putting the pointerEvents="none" prop there solved it.

Solution 4 - React Native

pointerEvents work only on View not on Text or TouchableOpacity.

Solution 5 - React Native

If you have an issue where even 'box-none' doesn't allow touch events to go to the children, you could do something like this

let setPointerEvents = 'none';
...
<View pointerEvents={setPointerEvents}>
   ...
   <TouchableOpacity onPress= {()=>this.set(false)}/>

</View>
<TouchableOpacity onPress= {()=>this.set(true)}/>
...
set(bool){
    switch(bool){
      case true:
        setPointerEvents= 'auto'
        break;
      case false:
        setPointerEvents= 'none'
        break;
   }
}

Kind of a hack, but it works.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMayank PatelView Question on Stackoverflow
Solution 1 - React NativeGil BirmanView Answer on Stackoverflow
Solution 2 - React NativeideView Answer on Stackoverflow
Solution 3 - React NativeJake CoxonView Answer on Stackoverflow
Solution 4 - React NativeSaikat SahaView Answer on Stackoverflow
Solution 5 - React NativeKyriakos XatzisavvasView Answer on Stackoverflow