Skip to content

Instantly share code, notes, and snippets.

@freddi301
Last active July 1, 2020 08:59
Show Gist options
  • Save freddi301/b9c0ef08bcb1f4e6252517be58ff9ddb to your computer and use it in GitHub Desktop.
Save freddi301/b9c0ef08bcb1f4e6252517be58ff9ddb to your computer and use it in GitHub Desktop.
Debounced value #react #hook
import React from "react";
export default function useDebounce<T>(value: T, delay: number) {
const [debouncedValue, setDebouncedValue] = React.useState(value);
React.useEffect(() => {
const timeout = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return function cleanup() {
clearTimeout(timeout);
};
}, [value, delay]);
return debouncedValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment