Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 379
Mayank Baiswar Avatar asked Jul 11 '18 09:07

Mayank Baiswar


People also ask

What are touchable interactions in react native?

Touchables​ The "Touchable" components provide the capability to capture tapping gestures, and can display feedback when a gesture is recognized. These components do not provide any default styling, however, so you will need to do a bit of work to get them looking nicely in your app.

What is hitSlop in react native?

hitSlop ​ This defines how far your touch can start away from the button. This is added to pressRetentionOffset when moving off of the button. The touch area never extends past the parent view bounds and the Z-index of sibling views always takes precedence if a touch hits two overlapping views.

What is Pressable in react native?

Pressable is a Core Component wrapper that can detect various stages of press interactions on any of its defined children.

How do I turn off touchable opacity in react native?

To simply disable the button, we can use the disabled prop in our button element and set its value to true . If we want to disable our button after clicking on it, We can disable it by using react's state.


2 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!

like image 111
Lurifaxel Avatar answered Nov 15 '22 20:11

Lurifaxel


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

like image 41
Steve Avatar answered Nov 15 '22 20:11

Steve