Skip to content

fix(useMap): skip rerenders for unchanged values - #1718

Open
sridharkalaibala wants to merge 1 commit into
react-hookz:masterfrom
sridharkalaibala:pr/fix-1655
Open

sridharkalaibala wants to merge 1 commit into
react-hookz:masterfrom
sridharkalaibala:pr/fix-1655

Conversation

@sridharkalaibala

Copy link
Copy Markdown

What is the current behavior, and the steps to reproduce the issue?

With const map = useMap([['count', 0]]), calling map.set('count', 0) schedules a render on every call even though the entry has not changed.

What is the expected behavior?

An unchanged entry should not schedule a render. Adding a key or changing its value should still render, and Map.set should retain its normal return value for chaining.

How does this PR fix the problem?

Check key presence and compare its previous value with Object.is before invoking the underlying Map.set. Only schedule a render when the entry changes. The presence check means a missing key with an undefined value is still inserted and triggers a render. NaN compares equal; 0 and -0 values remain distinct, as with React state.

The hook JSDoc documents the comparison and its object-reference consequence: mutating an object and setting the same reference no longer forces a render; callers should replace the value. No changes to clear/delete or exported types.

Fixes #1655.

Validation: nine added regression/control cases cover unchanged primitives, NaN, undefined, shared object identity, changed values, signed zero, distinct objects, and insertion of an undefined entry. Original implementation: four fail and 19 pass across the useMap DOM/SSR tests. With the fix, the complete suite passes: 541 tests in 117 files, including SSR. yarn fmt, yarn lint:fix, final yarn lint/yarn fmt:check, and yarn build pass. Formatting's unrelated checkout line-ending changes were restored; only the two useMap files are included. Node 24.19, pinned Yarn 4.17.1, immutable install; no dependency/lockfile changes. Prepared with AI assistance.

Checklist

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

useMap's set() should not re-render when setting a key to the same value it's currently set to

1 participant