Skip to content

Instantly share code, notes, and snippets.

@lazuee
Created February 14, 2026 14:35
Show Gist options
  • Select an option

  • Save lazuee/f90a574a55903a19f776ea6bc499a519 to your computer and use it in GitHub Desktop.

Select an option

Save lazuee/f90a574a55903a19f776ea6bc499a519 to your computer and use it in GitHub Desktop.
useDebouncedSubmit
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