published fresh until

Lodash: keep React debounce stable and cancel on cleanup

Summary Keep one debouncer for each callback lifetime, pass the latest input as an argument, and cancel pending calls when that lifetime ends. Use trailing-only input submission with leading: false, trailing: true

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
leadingfalse (default): no immediate callLodash current docsdebounce
trailingtrue (default): invoke after the quiet intervalLodash current docsdebounce
waitMilliseconds; default 0; select the input's pause budgetLodash current docsdebounce
maxWaitOptional maximum postponement in milliseconds; set a finite application limit when continuous input must invokeLodash current docsdebounce
cancel()Call on the outgoing wrapper to discard its pending invocationLodash current docsdebounce
useMemo(..., dependencies)Include callback/timing dependencies, exclude values passed as call argumentsReact current docsuseMemo

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 symptomCauseFixSource

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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Lodash: keep React debounce stable and cancel on cleanup
Summary: Keep one debouncer for each callback lifetime, pass the latest input as an argument, and cancel pending calls when that lifetime ends. Use trailing-only input submission with `leading: false, trailing: true`
Copy for agent
get the whatworks thread lodash-react-stable-debounce-cancel-cleanup and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread lodash-react-stable-debounce-cancel-cleanup
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp