Skip to content

fix(useUnmount): avoid writing to ref during render#727

Open
q121212 wants to merge 3 commits into
juliencrn:masterfrom
q121212:fix/useUnmount-ref-in-render
Open

fix(useUnmount): avoid writing to ref during render#727
q121212 wants to merge 3 commits into
juliencrn:masterfrom
q121212:fix/useUnmount-ref-in-render

Conversation

@q121212

@q121212 q121212 commented Jul 11, 2026

Copy link
Copy Markdown

Fixes #687

Problem

funcRef.current = func runs during render, which violates React's rules of refs. Per the React docs: "Do not write or read ref.current during rendering, except for initialization." React may discard a render before committing, leaving a stale ref value for the actual cleanup effect.

Fix

Move the ref assignment into a useEffect that only runs after the render is committed. The latest function reference is safely stored before the unmount cleanup executes.

q121212 added 2 commits July 11, 2026 17:34
…e reset

The options object ({ leading, trailing, maxWait }) was compared
by reference in useMemo/useEffect dependencies. Passing an inline
literal like { leading: true } created a new object each render,
resetting the debounce timer and preventing debouncing entirely.

Fix: destructure options into primitive values (leading, trailing,
maxWait) and use them directly in dependency arrays so recalculation
only occurs when option values actually change.

Fixes juliencrn#703
Updating funcRef.current during render violates React's rules of
refs. React may discard or restart a render, leaving a stale ref
value for the actual cleanup. This can cause unpredictable behavior
when the component unmounts.

Fix: move the ref assignment into a useEffect so it only runs
after the render is committed.

Fixes juliencrn#687
@changeset-bot

changeset-bot Bot commented Jul 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1f91d28

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
usehooks-ts Patch
www Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] useUnmount() writes to a ref unsafely during render

1 participant