React Native Prevent Double Tap

后端 未结 10 655
醉梦人生
醉梦人生 2020-12-30 02:22

I have a TouchableHighlight wrapping a Text block that when tapped, opens a new scene (which I\'m using react-native-router-flux).
It\'s all wo

10条回答
  •  一向
    一向 (楼主)
    2020-12-30 02:43

    What you're trying to do is you want to limit your on tap callbacks, so that they will only run ONCE.

    This is called throttling, and you can use underscore for that: Here's how:

    _.throttle(
        this.thisWillRunOnce.bind(this),
        200, // no new clicks within 200ms time window
    );
    

    Here's how my react component looks after all.

    class MyComponent extends React.Component {
      constructor(props) {
        super(props);
         _.throttle(
            this.onPressThrottledCb.bind(this),
            200, // no new clicks within 200ms time window
        );
      }
      onPressThrottledCb() {
        if (this.props.onPress) {
          this.props.onPress(); // this only runs once per 200 milliseconds
        }
      }
      render() {
        return (
          
            
            
          
        )
      }
    }
    

    I hope this helps you. In case you wanna learn more check this thread.

提交回复
热议问题