Skip to content

Instantly share code, notes, and snippets.

@ponyjackal
Created September 17, 2020 23:46
Show Gist options
  • Save ponyjackal/17923c313db6c8e10c4d20b1bce708dd to your computer and use it in GitHub Desktop.
Save ponyjackal/17923c313db6c8e10c4d20b1bce708dd to your computer and use it in GitHub Desktop.
useDebounce
import React, { useState, useEffect } from "react";
const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
};
}, [value, delay]);
return debouncedValue;
};
export default useDebounce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment