How to use throttle or debounce with React Hook?

后端 未结 17 1004
甜味超标
甜味超标 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:08

    If you are using it in handler, I am fairly certain this is the way to do it.

    function useThrottleScroll() {
      const savedHandler = useRef();
    
      function handleEvent() {}
    
      useEffect(() => {
        savedHandleEvent.current = handleEvent;
      }, []);
    
      const throttleOnScroll = useRef(throttle((event) => savedHandleEvent.current(event), 100)).current;
    
      function handleEventPersistence(event) {
        return throttleOnScroll(event);
      }
    
      return {
        onScroll: handleEventPersistence,
      };
    }
    

提交回复
热议问题