Perform debounce in React.js

前端 未结 30 2149
礼貌的吻别
礼貌的吻别 2020-11-22 04:11

How do you perform debounce in React.js?

I want to debounce the handleOnChange.

I tried with debounce(this.handleOnChange, 200) but it doesn\'t

30条回答
  •  忘掉有多难
    2020-11-22 04:37

    Did you try?

    function debounce(fn, delay) {
      var timer = null;
      return function() {
        var context = this,
          args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function() {
          fn.apply(context, args);
        }, delay);
      };
    }
    
    var SearchBox = React.createClass({
      render: function() {
        return ;
      },
    
      handleOnChange: function(event) {
        debounce(\\ Your handleChange code , 200);
      }
    });
    

提交回复
热议问题