The most-asked React hooks question. It checks that you truly understand useEffect cleanup — the cleanup is the debounce.
Mirror the input value into state. Every time value changes, schedule a timeout to copy it into debounced. The effect's cleanup clears the previous timeout, so rapid changes keep cancelling each other until things go quiet for delay ms — then the last value lands.
delay is in the dependency array, so changing it re-arms correctly.useDebouncedCallback wrapping the fn in a useRef'd timer.useRef here?" → you can, but the effect-cleanup version is the idiomatic one.Write useDebounce, then a useDebouncedCallback(fn, delay) variant that returns a stable debounced function.
import { useEffect, useState } from "react"; function useDebounce(value, delay = 300) { // return a debounced copy of `value` } // Usage: // const debouncedQuery = useDebounce(query, 400); // useEffect(() => { search(debouncedQuery); }, [debouncedQuery]);
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.