-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: search route without path and stale debounce
- Loading branch information
1 parent
dfce230
commit bebd490
Showing
4 changed files
with
30 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { MutableRefObject, useEffect, useRef } from 'react'; | ||
|
||
import { debounce } from '#src/utils/common'; | ||
|
||
const useDebounce = <T extends (...args: any[]) => unknown>(callback: T, time: number) => { | ||
const fnRef = useRef() as MutableRefObject<T | undefined>; | ||
const debounceRef = useRef( | ||
debounce((...rest) => { | ||
if (fnRef.current) fnRef.current(...rest); | ||
}, time), | ||
); | ||
|
||
useEffect(() => { | ||
fnRef.current = callback; | ||
}, [callback]); | ||
|
||
return debounceRef.current; | ||
}; | ||
|
||
export default useDebounce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters