React Dojo

Search

Search concepts, exercises and quizzes

utility

useDebounce

Delays updating a value until a given time has passed without changes. Ideal for search inputs and live filters.


useDebounce.ts
import { useState, useEffect } from "react"

export function useDebounce<T>(value: T, delay: number): T {
  const [debouncedValue, setDebouncedValue] = useState<T>(value)

  useEffect(() => {
    const timer = setTimeout(() => {
      setDebouncedValue(value)
    }, delay)

    return () => clearTimeout(timer)
  }, [value, delay])

  return debouncedValue
}
Was this helpful?
Sign in to give feedback