Skip to content

fix: preserve useSessionStorage updates for shared keys - #2736

Open
wanxiankai wants to merge 1 commit into
streamich:masterfrom
wanxiankai:fix/2604-session-storage-shared-key
Open

wanxiankai wants to merge 1 commit into
streamich:masterfrom
wanxiankai:fix/2604-session-storage-shared-key

Conversation

@wanxiankai

Copy link
Copy Markdown

Description

Fixes #2604.

When two useSessionStorage instances share a key in the same render tree, updating the first instance also rerenders the second. The persistence effect currently runs after every render, so the second instance writes its unchanged state over the update. For example, setting "updated" leaves "initial" in session storage.

Run the persistence effect when key, raw, or state changes. This preserves writes when switching keys or serialization modes while allowing the changed instance's value to remain in storage. Instances continue to maintain their own React state.

Add regression tests for shared keys in both JSON and raw modes, unrelated rerenders, key and mode changes, restricted storage access, empty serialized values, and the server fallback. Document the behavior of instances sharing a key.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Validation

  • Before the fix, both shared-key regression cases and the unrelated-rerender case fail: the stored update is overwritten.
  • Full Jest suite: 78 suites, 500 tests passed.
  • Focused hook coverage: 100% statements, branches, functions, and lines; all 8 focused tests pass.
  • Server fallback also passes separately with jest.config.node.ts.
  • Full ESLint run: 0 errors, 97 existing warnings in other files. The changed source and tests lint cleanly.
  • tsc --noEmit, CommonJS build, and ESM build pass.
  • git diff --check passes.

Checklist

  • Read the Contributing Guide
  • Perform a code self-review
  • Comment the code, particularly in hard-to-understand areas (not applicable: the implementation change only supplies the effect's dependencies)
  • Add documentation
  • Add hook's story at Storybook (not applicable: this fixes an existing hook without adding a new API)
  • Cover changes with tests
  • Ensure the test suite passes
  • Provide 100% tests coverage for useSessionStorage
  • Make sure code lints
  • Make sure types are fine

This branch has not been deployed

No deployments
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.

useSessionStorage persists existing value when multiple instances with the same key are rendered at once

1 participant