让lodash油门与ReduxAction一起工作

我正在使用最新的 Redux 工具包,我正在尝试限制 keydown 和 keyup 操作,但我不明白为什么它不起作用......我觉得它与调度有关,但我尝试使用也没有运气。

这不可能吗?

  // this does NOT get throttled
  const handleOnKeyDown = (e) => {
    e.persist();
    if (e.key === 'ArrowUp') {
      dispatch(setSelectedPage(pageAbove)); // not throttled
    } else if (e.key === 'ArrowDown') {
      dispatch(setSelectedPage(pageBelow)); // not throttled
    }
  };

  // this DOES get throttled
  const handleOnKeyDown = (e) => {
    e.persist();
    console.log('test');
  };

  const throttledOnKeyDown = _.throttle(handleOnKeyDown, 1000);

我也尝试了以下方法,但这也不起作用:

  const throttleSetSelectedPage = _.throttle((param) => dispatch(setSelectedPage(param)), 1000);

  const handleOnKeyDown = (e) => {
    e.persist();
    if (e.key === 'ArrowUp') {
      throttleSetSelectedPage(pageAbove);
    } else if (e.key === 'ArrowDown') {
      throttleSetSelectedPage(pageBelow);
    }
  };

以上是让lodash油门与ReduxAction一起工作的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>