Mayank Baiswar
Mayank Baiswar

Reputation: 595

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.

Upvotes: 8

Views: 5318

Answers (3)

Hemant Mali
Hemant Mali

Reputation: 29

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.

Upvotes: 2

Stephen
Stephen

Reputation: 1416

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

Upvotes: 3

Lurifaxel
Lurifaxel

Reputation: 714

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!

Upvotes: 14

Related Questions