We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent da2fbbb commit 481bc03Copy full SHA for 481bc03
‎2024 Prep/machine_coding/react/src/hooks/index.ts
@@ -1 +1,2 @@
1
export { useFetch } from './useFetchAsync'
2
+export { useDebounce } from './useDebounce'
‎2024 Prep/machine_coding/react/src/hooks/useDebounce.ts
@@ -0,0 +1,16 @@
+import { useState, useEffect } from 'react'
+
3
+const useDebounce = <T>(callBack: T, delay = 500) => {
4
+ const [debouncedText, setDebouncedText] = useState<string>('')
5
6
+ useEffect(() => {
7
+ // * add delay for setting the valye
8
+ const timeOut = setTimeout(() => {
9
+ setDebouncedText(debouncedText)
10
+ }, delay)
11
12
+ // * run clearTimeout in a cleanup function
13
+ return () => clearTimeout(timeOut)
14
+ }, [debouncedText, delay])
15
+}
16
+export { useDebounce }
0 commit comments