SHARE
TWEET

Untitled

a guest Aug 19th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useEffect, useState } from "react";
  2.  
  3. function useDebounce(value, delay) {
  4.     const [debouncedValue, setDebouncedValue] = useState(value);
  5.  
  6.     useEffect(() => {
  7.         const handler = setTimeout(() => {
  8.             setDebouncedValue(value);
  9.         }, delay);
  10.  
  11.         return () => {
  12.             clearTimeout(handler);
  13.         };
  14.     }, [value, delay]);
  15.  
  16.     return debouncedValue;
  17. };
  18.  
  19. function useDebouncedState(initialState, interval) {
  20.     const [value, setValue] = useState(initialState);
  21.     const debouncedValue = useDebounce(value, interval);
  22.  
  23.     return [value, setValue, debouncedValue];
  24. }
  25.  
  26. function useDebouncedEffect(value, interval, effect) {
  27.     const debouncedValue = useDebounce(value, interval);
  28.  
  29.     useEffect(() => effect(debouncedValue), [debouncedValue, effect]);
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top