lodash debounce in React functional component not working

前端 未结 3 1797
日久生厌
日久生厌 2021-02-01 04:03

I have a functional component built around the React Table component that uses the Apollo GraphQL client for server-side pagination and searching. I am trying to implement debou

3条回答
  •  被撕碎了的回忆
    2021-02-01 04:40

    To add onto Tholle's answer: if you want to make full use of hooks, you can use the useEffect hook to watch for changes in the filter and run the debouncedLoadData function when that happens:

    const { useState, useCallback, useEffect } = React;
    const { debounce } = _;
    
    function App() {
      const [filter, setFilter] = useState("");
      const debounceLoadData = useCallback(debounce(fetchData, 1000), []);
    
      useEffect(() => {
        debounceLoadData(filter);
      }, [filter]);
    
      function fetchData(filter) {
        console.log(filter);
      }
    
      return  setFilter(event.target.value)} />;
    }
    
    ReactDOM.render(, document.getElementById("root"));
    

提交回复
热议问题