fix(useMap): skip rerenders for unchanged values - #1718
Open
sridharkalaibala wants to merge 1 commit into
Open
sridharkalaibala wants to merge 1 commit into
sridharkalaibala wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the current behavior, and the steps to reproduce the issue?
With
const map = useMap([['count', 0]]), callingmap.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.setshould retain its normal return value for chaining.How does this PR fix the problem?
Check key presence and compare its previous value with
Object.isbefore 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.NaNcompares equal;0and-0values 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, finalyarn lint/yarn fmt:check, andyarn buildpass. 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
useMap'sset()should not re-render when setting a key to the same value it's currently set to #1655