Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent parent from being clickable while allowing child to capture touches

In react-native sometimes it is useful to prevent a parent from capturing and handling touch / click events while allowing its children to receive and react to the events. Specifically, this is useful when using a <View/> or <ImageBackground/> container that acts as a transparent background to its child elements (example: gradient based shadows or wrapper views used to center elements of absolute positioning).

like image 466
Wassim Gr Avatar asked Oct 15 '25 03:10

Wassim Gr


1 Answers

React Native offers a pointerEvents prop for <View/> elements which can be set to 'none' in order to stop reacting to touches and clicks, however the trick to allowing the children to still be touchable is to set the pointerEvents prop to 'box-none' instead of 'none'.

Warning: In contrast to regular CSS, pointerEvents is ` prop and not a style. So to use it, you can do

<View style={styles.parentWithoutTouchEvents} pointerEvents='box-none'>
   <View style={styles.touchableChild}>
   </View>
</View>
like image 154
Wassim Gr Avatar answered Oct 16 '25 16:10

Wassim Gr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!