【问题标题】:Throttle with dispatch带调度的节流阀
【发布时间】:2020-08-02 12:41:16
【问题描述】:

我有一个调度动作的事件监听器。

window.addEventListener('resize', () => {
    store.dispatch(screenResize());
})

我正在尝试用 lodash 节流(或去抖动)

问题是,我应该这样做

const throttledScreenResize =  _.throttle(screenResize(), 250)

window.addEventListener('resize', () => {
    store.dispatch(throttledScreenResize);
})

或

const throttledScreenResize =  _.throttle(() => store.dispatch(screenResize()), 250)

window.addEventListener('resize', throttledScreenResize)

或者两者都不是?然后呢?

谢谢

【问题讨论】:

  • 你尝试过这些吗?
  • @UtsavPatel 不,我没有

标签: react-redux lodash store throttling debounce


【解决方案1】:

采取第二种方法:

在_throttle 中调用store.dispatch(..)。这将确保 store.dispatch 每 250 毫秒执行不超过一次

const throttledScreenResize =  _.throttle(() => store.dispatch(screenResize()), 250)

window.addEventListener('resize', throttledScreenResize)

在第一种方法中: 在每个 resize 事件上调用 store.dispatch。

【讨论】:

  • 谢谢,我以为会这样,但想确认一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 2017-03-22
  • 2021-12-10
  • 1970-01-01
  • 2020-01-22
  • 2012-05-11
  • 1970-01-01
相关资源
最近更新 更多