Can I disable a View component in react native?
Asked Answered
R

3

63

My app screen has a View component with few Text Inputs. I cannot disable text inputs. Is there a way that I can disable complete View?

P.S.: By Disabling a View component I mean that the component renders but becomes unresponsive of any action.

Reba answered 27/9, 2016 at 8:42 Comment(0)
C
151

You can use pointerEvents:

<View pointerEvents="none">
  ...
</View>

This will make the view unresponsive to touch events.

You can use something like

<View pointerEvents={myCondition ? 'none' : 'auto'}>

Crop answered 27/9, 2016 at 8:45 Comment(5)
Thanks for this solution.Reba
Can we give it conditinally?Rostock
Yes you can use something like <View pointerEvents={myCondition ? 'none' : 'auto'}>Crop
super useful prop, how i never heard of it?Caltrop
thanhks, this is quickly.Benares
S
29

Adding to Kerumen's answer, in some rare cases:

<View pointerEvents={myCondition ? 'none' : 'auto'}>
  ...
</View>

You might need to wrap it in an anonymous function:

<View pointerEvents={() => myCondition ? 'none' : 'auto'}>
  ...
</View>
Sasnett answered 19/12, 2017 at 15:40 Comment(3)
It looks like your code block didn't format the way you intended. Also, in what rare cases would you need to wrap* it in an anonymous function?Florin
honestly, I don't but for some reason, it didn't work until I warp it in a function, I am investigating to find out the reasonSasnett
This worked perfectly!Cully
C
0

Create a TouchableOpacity in a space where you want to make it unresponsive to any action. like this :

<TouchableOpacity style={{ width : 40 , height : 40}} activeOpacity={1}>
</TouchableOpacity>

this area doesnt have any action from parents component

Ceramics answered 25/5, 2023 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.