diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2022-05-05 07:01:55 +0000 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2022-05-05 07:27:44 +0000 |
commit | 0d17912972de6b9e8b08aedf141260413c60d7fd (patch) | |
tree | f0a56bfea9204dd3067c43e2667e3d70e581d183 /src/console/hooks | |
parent | bc890c55bfbd6d1aa4ece5b0ef96a9ab6d358ad7 (diff) |
Use useDebounce
Diffstat (limited to 'src/console/hooks')
-rw-r--r-- | src/console/hooks/useDebounce.ts | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/console/hooks/useDebounce.ts b/src/console/hooks/useDebounce.ts new file mode 100644 index 0000000..838ff34 --- /dev/null +++ b/src/console/hooks/useDebounce.ts @@ -0,0 +1,19 @@ +import React from "react"; + +const useDebounce = <T extends unknown>(value: T, delay: number) => { + const [debouncedValue, setDebouncedValue] = React.useState(value); + + React.useEffect(() => { + const timer = setTimeout(() => { + setDebouncedValue(value); + }, delay); + + return () => { + clearTimeout(timer); + }; + }, [value, delay]); + + return debouncedValue; +}; + +export default useDebounce; |