fix(useStorageValue): compose updates before rerender - #1721
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?
Three consecutive functional increments from a stored/default value of 0 all receive 0 and leave the value at 1. The same stale value can be used when two mounted hooks update the same storage key before React renders.
Closes #1507.
What is the expected behavior?
Each functional update should receive the latest accepted value. Three increments should receive 0, 1 and 2 and leave both storage and the rendered value at 3.
How does this PR fix the problem?
Keep the current-value ref synchronized when the hook accepts a parsed storage notification or fetch result, together with the React state update. Previously, useSyncedRef refreshed that value only during a render. The shared implementation covers localStorage and sessionStorage and preserves the existing serialization, notification and stable-action behavior. A rejected serialization or failed write does not advance the value. Add JSDoc describing functional-update behavior.
This composes synchronous updates among mounted hooks in the same document; it does not introduce atomic read-modify-write across browser tabs.
Validation
yarn fmt,yarn lint:fix,yarn test,yarn buildpass; finalyarn fmt:checkandyarn lintalso pass.Checklist
AI assistance: implemented and validated with OpenAI Codex.