简体   繁体   中英

TouchableOpacity onPress not working on Android

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.

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!

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! :)

import {TouchableOpacity} from 'react-native' 
works fine in both android and iOS


import {TouchableOpacity} from 'react-native-gesture-handler'
Does not work in android, but works in iOS.

So better to user react-native imports.

onPress event is not calling in android if TouchableOpacity imported from react-native-gesture-handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM