Is there a way in which I can ignore touch events on Text in React Native?
React NativeReact 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.