【问题标题】:Debounce doesn't trigger function去抖不触发功能
【发布时间】:2016-12-22 08:19:07
【问题描述】:

我正在开发一个 React 组件,我希望将 debounce 绑定到输入。我的代码如下:

class Form extends React.Component {
  constructor() {
    ...
    this.deb = this.deb.bind(this);
  }
  ...

  this.deb() {
    debounce(() => { console.log('bam'); }, 400, false);
  }

  render() {
    return (
      <input
        onChange={this.deb}
        />
    )
  }
}

没有debounce in this.deb 一切正常。我能做什么?

【问题讨论】:

    标签: javascript reactjs lodash


    【解决方案1】:

    debounce 会为您传入的函数创建一个新的去抖动版本,因此在这种情况下您只需在构造函数中执行一次:

    class Form extends React.Component {
      constructor(props) {
        super(props);
        this.deb = debounce(this.deb.bind(this), 400, false);
      }
    
      deb() {
        console.log('bam');
      }
    
      render() {
        return <input onChange={this.deb} />;
      }
    }
    

    【讨论】:

    • 效果很好,我知道这很简单;)非常感谢@Tholle!
    • @TomekBuszewski 太棒了!没问题。 :)
    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2015-03-03
    • 2015-07-25
    • 2023-01-10
    • 2021-06-30
    相关资源
    最近更新 更多