Repository navigation
fix(useStorageState): reset to the default value when another tab clears the storage - #482
Conversation
🦋 Changeset detectedLatest commit: efa112d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
🟢 Approval recommended
The change is minimal, aligns with the Web Storage event semantics (key: null on clear), and is backed by focused regression tests.
Pull request overview
Fixes useStorageState so it correctly reacts when another tab clears storage via localStorage.clear() (which emits a storage event with key: null), ensuring the hook resets to defaultValue (or undefined) without requiring a refresh.
Changes:
- Treat
StorageEvent.key == nullas a relevant event and trigger a snapshot refresh. - Add regression tests covering
localStorage.clear()behavior, cross-storage clearing, observed-key removal, and unrelated-key removal. - Add a patch changeset describing the fix.
File summaries
| File | Description |
|---|---|
| packages/react-simplikit/src/hooks/useStorageState/useStorageState.ts | Updates the storage-event filter to also handle key: null events (storage clear). |
| packages/react-simplikit/src/hooks/useStorageState/useStorageState.spec.ts | Adds targeted tests to validate reset/preserve behavior for clear/remove events across tabs and storage areas. |
| .changeset/fix-storage-clear-sync.md | Patch changeset documenting the bug fix. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #482 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 69 69
Lines 2233 2233
Branches 717 718 +1
=========================================
Hits 2233 2233 🚀 New features to boost your workflow:
|
hyesungoh
left a comment
There was a problem hiding this comment.
Thanks to your contribution! 👍
Overview
다른 탭에서
localStorage.clear()를 호출했을 때useStorageState에 이전 값이 남는 문제를 수정합니다. 이제 새로고침이나 반환된refresh호출 없이defaultValue로 돌아가며, 기본값이 없으면undefined가 됩니다.재현 방법
같은 origin의 탭 A에서
localStorage.setItem('name', JSON.stringify('Alice'))로 값을 저장한 뒤 아래 hook을 마운트합니다.탭 B에서
localStorage.clear()를 실행합니다.저장소는 비워지지만, 수정 전에는 탭 A의
name이'Alice'로 남습니다. 기대값은'Guest'입니다.clear()로 전달되는 storage 이벤트의key는null입니다. 기존 구독 조건은 관찰 중인 key와 정확히 일치하는 이벤트만 처리하므로 전체 삭제를 놓칩니다. (HTML 표준)변경 내용
key가null인 이벤트에도 구독자를 깨워 기존 snapshot 로직으로 저장소를 다시 읽습니다.newValue를 직접 적용하지 않으므로, 다른 종류의 저장소가 비워져도 hook이 사용하는 저장소의 값은 유지됩니다.검증
useStorageState테스트 53개가 모두 통과했습니다. 기존 SSR 검증도 포함됩니다.localStorage.clear()를 실행했습니다.isTrusted: true,key: null이벤트가 도착한 뒤 기본값이 있는 hook은 기본값으로, 없는 hook은undefined로 갱신됐습니다.yarn run test:coverage: 통과. 라이브러리 테스트 579개 통과,useStorageState.ts와storage.ts의 실행 구문 커버리지 100%.yarn run test:compiled: 581개 통과.test:format,test:lint,test:type,test:docs,test:skill,test:codemod,verify:compiler,verify:pack: 통과.로컬 검증 환경: Node 24.19.0, Yarn 4.18.0, React 19.1.0.
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?