6

I have a simple piece of code that's just a TouchableOpacity with a onLongPress prop, but it does not seem to be working.

<TouchableOpacity delayLongPress={10} onLongPress={()=>{console.log("pressed")}} activeOpacity={0.6}>
  <Text>BUTTON</Text>
</TouchableOpacity>

I've tried removing the delay prop but that still doesn't work. Changing onLongPress to onPress does seem to work however, but I want the long press functionality. I'm testing this on an Android simulator.

Smarticles101
  • 1,822
  • 1
  • 14
  • 28
Nerdragen
  • 2,976
  • 2
  • 11
  • 22

3 Answers3

11

According to this issue this happens randomly, after testing on a real device with React Native Debugger enabled. Disabling React Native Debugger will make your problem go away.

romin21
  • 1,532
  • 1
  • 14
  • 35
  • 3
    `delayLongPress={10}` delays the function call for 10 milliseconds, that's why it looks like it's instant. Try setting it to a value > 1000 for example. – romin21 Nov 28 '18 at 21:10
  • That doesn't matter, onLongPress is not being called. – Nerdragen Nov 29 '18 at 23:04
4

if you want to show a view on long press and hide it on release :

<TouchableOpacity
    onPress={this._onPress}
    onLongPress={this._onLongPress}
    onPressOut={this._onPressOut}
>
            ....
</TouchableOpacity>



_onLongPress = () => {
    this.setState({
        modalVisible: true
    })
}
_onPressOut = () => {
    this.setState({
        modalVisible: false
    })
}
AlainIb
  • 4,544
  • 4
  • 38
  • 64
-3

You can test on a IOS simulator or in the release package.

zhou Dai
  • 341
  • 2
  • 4