简体   繁体   English

我如何正确地去抖动 mapDispatchToProps 或 redux 动作?

[英]How i can correctly debounce mapDispatchToProps or redux action?

I have redux action:我有 redux 动作:

export const setSearchText = payload => ({
  type: SEARCH_ACTION,
  payload,
});

And following mapDispatchToPros:并遵循 mapDispatchToPros:

const mapDispatchToProps = dispatch => ({
  onChange: (...args) => setSearchText(...args),
});

How i can it debounce?我怎样才能去抖动?

const mapDispatchToProps = dispatch => ({
  onChange: (...args) =>{
    setTimeout(()=>dispatch(setSearchText(...args)),1000);
  }
});

Here the debounce time is set to 1 sec这里的去抖时间设置为 1 秒

Hope this code works希望这段代码有效

  onChange: (...args) => debounce(() => dispatch(setSearchText(...args)), 500)

This is the debounce function.这是去抖 function。

const debounce = (func, wait) => {
  let timeout;
  return function() {
    const context = this;
    const args = arguments;
    const later = function() {
      timeout = null;
      func.apply(context, args);
    };
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);
  };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM