What this thread does
Do first: create a stable _.debounce wrapper with leading: false, trailing: true, pass each new input value to it, and register its cancel() in effect cleanup.
Short answer
As of November 2026, Lodash 4.18.1 and current React Hook docs describe this lifecycle. Debouncing postpones invocation. Preserve the wrapper across input renders. Lodash uses the latest arguments.
Choose / don't choose
- Trailing-only: choose for submission after a pause; don't enable the leading edge when the first keystroke must not submit.
maxWait: choose when continuous typing must still trigger periodic submission; omit it when only a quiet interval should submit.- Callback replacement: choose a new wrapper when its actual callback or timing policy changes; cancel the previous wrapper. Don't put the input text itself in memo dependencies if it can be an argument.
Not for: React effect update loops; see react-too-many-re-renders-maximum-update-depth-useeffect.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
leading | false (default): no immediate call | Lodash current docs | debounce |
trailing | true (default): invoke after the quiet interval | Lodash current docs | debounce |
wait | Milliseconds; default 0; select the input's pause budget | Lodash current docs | debounce |
maxWait | Optional maximum postponement in milliseconds; set a finite application limit when continuous input must invoke | Lodash current docs | debounce |
cancel() | Call on the outgoing wrapper to discard its pending invocation | Lodash current docs | debounce |
useMemo(..., dependencies) | Include callback/timing dependencies, exclude values passed as call arguments | React current docs | useMemo |
Order of operations
1. Make submit stable in its owner, or include its changing identity in dependencies. It must not close over input state that the wrapper is expected to keep current. 2. Create the wrapper without invoking it during render. Illustrative policy: wait 300 ms and maxWait 1000 ms are application choices, not defaults:
const debounced = useMemo(() => _.debounce(
value => submit(value), 300,
{ leading: false, trailing: true, maxWait: 1000 }
), [submit]);
useEffect(() => () => debounced.cancel(), [debounced]);
// In the input change handler:
debounced(nextValue);
3. Extract the current input string in the event handler; pass it rather than relying on state read by an older closure. Memoizing with each input value creates separate timer owners instead of one burst. 4. React calls the old effect cleanup before setup with changed dependencies and on removal. Cancel that effect's wrapper during cleanup, including callback replacement and unmount. Development Strict Mode adds a setup/cleanup cycle; cleanup must tolerate it. Effect lifecycle.
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|
Missing
cancel()cannot undo a request that already started; cancellation of network operations needs the transport's own lifecycle.- React documents memoization as an optimization and can discard a memo cache. Long-lived external ownership must not depend on an everlasting cache; the recipe describes a mounted callback lifetime, not a global singleton.
Checked
2026-11-12; Lodash 4.18.1 release and debounce reference, React useMemo/useEffect references read. Alexandre Annic’s February 13, 2019 question records the historical Hook/debounce problem. No application commands executed.
Started from a post by @Lodash, packaged by the editor. Original post