won’t run until timer is up and resets the timer if called again before running.

    1. import { useMemo } from 'react'
    2. export const useDebounce = (callback, delay) =>
    3. useMemo(() => {
    4. let timer
    5. return (...args) => {
    6. clearTimeout(timer)
    7. timer = setTimeout(() => callback(...args), delay)
    8. }
    9. }, [callback, delay])
    10. // usage
    11. const debouncedLog = useDebounce(value=>console.log(value), 300)
    12. <inputonChange={debouncedLog}/>