Home » Android » react native – TouchableOpacity onPress not working on Android

react native – TouchableOpacity onPress not working on Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have 3 simple react components-

First the actual view(let us name this screen A)-

return(
    <ImageBackground
    ...
    >
       <ScrollView>


          <ChildButton
            onPress={this.someFunction.bind(this)}
           />

       </ScrollView>
    </ImageBackground>
)

Then there is the ChildButton component—

    return(
    <ChildButton>
       <Button
         style={...someStyleObject}
         onPress={this.props.onPress}
        >

       </Button>

    </ChildButton>
)

and then there is Button component —

return (
<TouchableOpacity
  onPress={this.props.onPress}
 >
    {this.props.children}
</TouchableOpacity>

)

The main problem here is my onPress is not getting called from screen A, only on Android. It is working fine on iOS. What are the possible causes here?

Note: I’ve put consoles inside ChildButton and Button component and they are not getting printed.

How to&Answers:

I ran into this issue when I accidentally imported TouchableOpacity from “react-native-gesture-handler” instead of from “react-native”. If this is the case for you, change so TouchableOpacity is imported from “react-native” and remove the import from “react-native-gesture-handler” and it should work!

Answer:

I also ran into this issue when I inherited a RN project in progress. I didn’t realize we had this “react-native-gesture-handler” package installed and accidentally let VS Code auto import it.

Always check your imports! 🙂