【问题标题】:ReactJS: debounce a function that has as an argument a state value; what's the best way to do it?ReactJS:去抖动一个以状态值作为参数的函数;最好的方法是什么?
【发布时间】:2020-04-15 22:55:45
【问题描述】:

我已经访问了这个链接并尝试了一些例子:Perform debounce in React.js

一点上下文:我正在构建一个我想在 NPM 上部署的搜索框。每次用户键入时,都会调用一个 prop 函数 onSearch。这允许程序员在需要时获取新数据。

问题:每个输入的字符都会触发 onSearch,但这不是最优的,所以我想去抖动它。

我想按照其中一篇文章的建议做:

import React, { useCallback } from "react";
import { debounce } from "lodash";

const handler = useCallback(debounce(someFunction, 2000), []);

const onChange = (event) => {
    // perform any event related action here

    handler();
 };

我的问题是我需要将一个参数传递给“someFunction”,而那个参数是一个状态(一个字符串):

const [searchString, setSearchString] = React.useState("");

经过各种尝试,我终于找到了解决方案。回想过去我是如何消除窗口调整大小事件的,我遵循了或多或少相同的模式。我通过将事件侦听器附加到窗口对象并在调度事件时向事件添加属性来做到这一点。它有效,但它是一个好的解决方案吗?有没有更好的方法来实现这一点?

  React.useEffect( ()=> {

    // This will contain the keyword searched when the event is dispatched (the value is stored in event.keyword)
    // the only function dispatching the event is handleSetSearchString
    // It's declared at this level so that it can be accessed from debounceDispatchToParent
    let keyword = "";

    // This function contains the onSearch function that will be debounced, inputDebounce is 200ms
    const debounceDispatchToParent = debounce(() =>
      onSearch(keyword,  isCached("search-keyword-" + keyword)), inputDebounce);

    // This function sets the keyword and calls debounceDispatchToParent
    const eventListenerFunction = (e) => {
      // the event has a property attached that contains the keyword
      // store that value in keyword
      keyword = e.keyword;
      // call the function that will debounce onSearch
      debounceDispatchToParent();
    }

    // Add the listener to the window object
    window.addEventListener("dispatchToParent", eventListenerFunction, false);

    // Clean up
    return ()=> window.removeEventListener("dispacthToParent", eventListenerFunction);
  }, []);

然后每次用户输入我调用handleSetSearchString:

  const handleSetSearchString = keyword => {

    keyword = keyword.toLowerCase();
    // If the string is longer than the minimum characters required to trigger a filter/search
    if (keyword.length > minChars) {
      // Here I create the event that contains the keyword
      const event = new Event("dispatchToParent");
      event.keyword = keyword;
      window.dispatchEvent(event);

    } else if (keyword.length === 0) {
      // If the string is empty clear the results
      setFilteredItems([]);
    }
    setSearchString(keyword);

  };

【问题讨论】:

    标签: javascript reactjs debounce usecallback


    【解决方案1】:

    由于debounceuseCallback 都返回一个函数,你可以直接传递它。

    const handler = useCallback(debounce(someFunction, 2000), []);
    
    const onChange = (event) => {
       // perform any event related action here
    
       handler(argument1, argument2, ...args);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多