Skip to content

Commit

Permalink
feat: create useDebounce hook (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
hudy9x authored Dec 1, 2023
1 parent c1adeec commit 9379abf
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/ui-app/app/_hooks/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DependencyList, useEffect, useRef } from "react"

export const useDebounce = (cb: () => void, dependencies: DependencyList, time?: number) => {
const timeout = useRef(0)

useEffect(() => {
timeout.current = setTimeout(() => {
cb()
}, time || 300) as unknown as number

return () => {
if (timeout.current) {
console.log('debounce run')
clearTimeout(timeout.current)
}
}
}, dependencies)
}

0 comments on commit 9379abf

Please sign in to comment.