React Native: View onPress does not work

React NativeReact Native-Android

React Native Problem Overview


I'm facing a weird problem. In my react native app, if I set onPress event to View it is not triggered but if I set the same to Text inside View, it fires. What am I missing here?

<View style={{backgroundColor: "red", padding: 20}}>
  <Text onPress={()=> {
    console.log('works');
    }
  }>X</Text>
</View>


<View style={{backgroundColor: "red", padding: 20}} onPress={()=> {
    console.log('does not work');
    }
  }>
  <Text>X</Text>
</View>

Why is this so? Is this an issue with React Native? I'm using version 0.43

React Native Solutions


Solution 1 - React Native

You can use TouchableOpacity for onPress event. View doesn't provide onPress prop.

<TouchableOpacity style={{backgroundColor: "red", padding: 20}} onPress={()=> {
    console.log('does not work');
    }
  }>
  <Text>X</Text>
</TouchableOpacity>

Solution 2 - React Native

You can wrap the view with a TouchableWithoutFeedback and then use onPress and friends like usual. Also you can still block pointerEvents by setting the attribute on on the child view, it even blocks pointer events on the parent TouchableWithoutFeedback, its interesting, this was my need on Android, I didn't test on iOS:

https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html

<TouchableWithoutFeedback onPressIn={this.closeDrawer}>
    <Animated.View style={[styles.drawerBackground, styleBackground]} pointerEvents={isOpen ? undefined : 'none'} />
</TouchableWithoutFeedback>

Solution 3 - React Native

Alternatively you can also provide onStartShouldSetResponder to your view, like so:

<View onStartShouldSetResponder={() => console.log("View click")}>
  // some code here
</View>

Solution 4 - React Native

You can use TouchableOpacity, TouchableHighlight, TouchableNativeFeedback, to achieve this. View component doesn't provide onPress as props. So you use these instead of that.

<TouchableNativeFeedback
        onPress={this._onPressButton}
</TouchableNativeFeedback>

OR

<TouchableHighlight onPress={this._onPressButton}>
</TouchableHighlight>

OR

<TouchableOpacity onPress={this._onPressButton}>
</TouchableOpacity>

Solution 5 - React Native

onPress doesn't work on <View> tag use <TouchableOpacity> instead of View

<View>
<TouchableOpacity onPress={() => 'call your function here'}>
</TouchableOpacity>
</View>

Solution 6 - React Native

A new pressable component is provided in 0.67 of react native, which can solve your problem. It runs anywhere enter image description here

Solution 7 - React Native

well we can make the View have a onPress props onStartShouldSetResponder and onResponderGrant

<View
    onStartShouldSetResponder={() => true}
    onResponderGrant={() => console.log("view pressed")}
  >
</View>

Solution 8 - React Native

You can use TouchableOpacity for that purpose

<TouchableOpacity onPress={() => {your code here}}>
    //Your inner views here
</TouchableOpacity>

Solution 9 - React Native

In View onPress will not work because onPress event is not supported in view tag.That is why it is not working but you can go to this link https://reactnative.dev/docs/view

Solution 10 - React Native

2021

> If you're looking for a more extensive and future-proof way to handle touch-based input, check out the Pressable API.

Source: https://reactnative.dev/docs/touchablewithoutfeedback

Solution 11 - React Native

For anybody who's lookig for a solution to this, as of RN 0.63, there is a new Pressabe api. It might have rolled out a couple versions earlier but it works great for such use cases.

<Pressable onPress={onPressFunction}>
    <Text onPress={() => {
        console.log('works');
    }}>X</Text>
</Pressable>

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
QuestionmehulmptView Question on Stackoverflow
Solution 1 - React NativeAhsan AliView Answer on Stackoverflow
Solution 2 - React NativeNoitidartView Answer on Stackoverflow
Solution 3 - React NativeSydney C.View Answer on Stackoverflow
Solution 4 - React NativeNaleen DissanayakeView Answer on Stackoverflow
Solution 5 - React NativebismaView Answer on Stackoverflow
Solution 6 - React NativeGetView Answer on Stackoverflow
Solution 7 - React Nativethunder775View Answer on Stackoverflow
Solution 8 - React NativeAIKView Answer on Stackoverflow
Solution 9 - React Nativevishal SharmaView Answer on Stackoverflow
Solution 10 - React NativeIqbalView Answer on Stackoverflow
Solution 11 - React NativeAjay BhargavView Answer on Stackoverflow