如何去除多个可点击按钮

时间:2017-07-12 11:04:55

标签: javascript react-native lodash debouncing

目前,我正在使用lodash library进行辩论...

<Button
  onPress={_.debounce(
        () => {
               navigation.goBack()
              },
              500,
              {
                leading: true,
                trailing: false,
               }
   )}
   title="Back"
/>

当我点击一个链接时,这可以预测,但是如果我点击两个可点击的区域...(见下面的gif),会发生以下情况......

issue

1 个答案:

答案 0 :(得分:0)

从“渲染”方法移动去抖动函数的定义,如下所示:

export default class YourClassName extends Component {
  constructor() {
    super();

    this.debouncedOnPressHandler = _.debounce(
      () => { navigation.goBack() },
      500,
      {
        leading: true,
        trailing: false,
      }
    )
  }

  render() {
    return (
      <div>
        <Button onPress={this.debouncedOnPressHandler} title="Back A" />
        <Button onPress={this.debouncedOnPressHandler} title="Back B" />
      </div>
    );
  }
}