Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

452 B
Raw Blame
'use client'

import { debounce } from 'lodash'
import { useEffect, useMemo, useRef } from 'react'

export const useDebounce = (callback: () => void, delay: number = 1000) => {
  const ref = useRef<any | null>(null)

  useEffect(() => {
    ref.current = callback
  }, [callback])

  const debouncedCallback = useMemo(() => {
    const func = () => {
      ref.current?.()
    }

    return debounce(func, delay)
  }, [])

  return debouncedCallback
}