How to use throttle or debounce with React Hook?

后端 未结 17 1005
甜味超标
甜味超标 2020-11-30 04:27

I\'m trying to use the throttle method from lodash in a functional component, e.g.:

const App = () => {
  const [value, setValue         


        
17条回答
  •  鱼传尺愫
    2020-11-30 05:07

    Using lodash's debounce function here is what I do:

    import debounce from 'lodash/debounce'
    
    // The function that we want to debounce, for example the function that makes the API calls
    const getUsers = (event) => {
    // ...
    }
    
    
    // The magic!
    const debouncedGetUsers = useCallback(debounce(getUsers, 500), [])
    

    In your JSX:

    
    

提交回复
热议问题