Search code examples
javascriptreactjsreact-nativeonpress

Is there something like onPressAndHold in react native?


I made a custom number counter in react native: when you press + it increases the state when you press - it reduces the state value; the issue is that for someone to reach 60 from 0 it take a very long time using onPress, is there something that i could use to have a very long press.

onLongPress did not work, thank you.


Solution

  • you can use this

    <TouchableOpacity onPressIn={this.addOne} onPressOut={this.stopTimer}>
         <Icon name="plus-circle" size={30} color={'white'} />
    </TouchableOpacity>
    
    constructor() {
        super();
        this.state = {
          number: 0,
        };
        this.timer = null;
        this.addOne = this.addOne.bind(this);
        this.stopTimer = this.stopTimer.bind(this);
      }
    
      addOne() {
        this.setState({number: this.state.number+1});
        this.timer = setTimeout(this.addOne, 200);
      }
    
      stopTimer() {
        clearTimeout(this.timer);
      }