Created
February 14, 2026 14:35
-
-
Save lazuee/f90a574a55903a19f776ea6bc499a519 to your computer and use it in GitHub Desktop.
useDebouncedSubmit
This file contains hidden or 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
| import { useCallback, useEffect, useRef } from "react"; | |
| export const useDebouncedSubmit = <TArgs extends Array<any>>(onSubmit: (...args: TArgs) => Promise<any> | void, delay = 400) => { | |
| const timeoutRef = useRef<number | null>(null) | |
| useEffect(() => { | |
| return () => { | |
| if (timeoutRef.current !== null) clearTimeout(timeoutRef.current) | |
| } | |
| }, []) | |
| return useCallback(async(...args: TArgs) => { | |
| if (timeoutRef.current !== null) clearTimeout(timeoutRef.current) | |
| timeoutRef.current = setTimeout(() => { | |
| onSubmit(...args) | |
| }, delay) | |
| }, [onSubmit, delay]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment