Ledger Nano X - The secure hardware wallet
SHARE
TWEET

debounce hook

Aliendreamer May 19th, 2020 920 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { useEffect, useState } from "react";
  2.  
  3. const useDebounce = (value, delay) => {
  4.     // State and setters for debounced value
  5.     const [debouncedValue, setDebouncedValue] = useState(value);
  6.  
  7.     useEffect(
  8.         () => {
  9.             // Update debounced value after delay
  10.             const handler = setTimeout(() => {
  11.                 setDebouncedValue(value);
  12.             }, delay);
  13.  
  14.             // Cancel the timeout if value changes (also on delay change or unmount)
  15.             // This is how we prevent debounced value from updating if value is changed ...
  16.             // .. within the delay period. Timeout gets cleared and restarted.
  17.             return () => {
  18.                 clearTimeout(handler);
  19.             };
  20.         },
  21.         [value, delay] // Only re-call effect if value or delay changes
  22.     );
  23.  
  24.     return debouncedValue;
  25. };
  26.  
  27. export default useDebounce;
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