Can I disable a View component in react native?

33,050

Solution 1

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'}>

Solution 2

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>
Share:
33,050
user3300203
Author by

user3300203

Updated on August 28, 2021

Comments

  • user3300203
    user3300203 almost 3 years

    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.

  • user3300203
    user3300203 over 7 years
    Thanks for this solution.
  • Dhrupal
    Dhrupal over 6 years
    Can we give it conditinally?
  • Kerumen
    Kerumen over 6 years
    Yes you can use something like <View pointerEvents={myCondition ? 'none' : 'auto'}>
  • Joey Harwood
    Joey Harwood over 6 years
    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?
  • Admin
    Admin over 6 years
    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 reason
  • Nicolas Silva
    Nicolas Silva over 3 years
    super useful prop, how i never heard of it?