Fix useLocalStorage Hook to Sync State Across Tabs - #8998
Conversation
|
@adrish-mage is attempting to deploy a commit to the jhasourav07's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
This pull request addresses the issue of state synchronization across tabs by implementing a storage event listener in the useLocalStorage hook. This is similar to the previous decision made regarding syncing contribution goals with MongoDB, which aimed to enhance user experience by ensuring data persistence across devices. While this PR focuses on local storage synchronization, it’s important to consider the broader implications of state management in our application. Thank you for your contribution, and I look forward to your updates! |
📦 Next.js Bundle Size Report (Gzipped Sizes)
📊 Summary of Totals
|
|
@JhaSourav07 lemme know if any changes are required ! |
Aamod007
left a comment
There was a problem hiding this comment.
Great fix! Adding a \storage\ event listener to \useLocalStorage.ts\ perfectly solves the synchronization issue across multiple tabs. It's great that you only react to the specific key matching the hook's invocation.
The tests you've added in \useLocalStorage.test.ts\ to dispatch simulated \StorageEvent\s with both matching and non-matching keys perfectly validate this behavior.
Labels applied:
- type:bug: Fixes state desynchronization across tabs.
- level:intermediate: Requires handling cross-tab \StorageEvent\ listeners within a React hook lifecycle.
- quality:clean: Simple, effective logic backed by good unit tests.
|
🎉 Congratulations @adrish-mage! Your PR has been successfully merged. 🚀 Thank you for contributing to CommitPulse. Your work helps us build a better tool for the community.
Keep building! 💻✨ |
Description
Fixes #8996
Adds a
storageevent listener to theuseLocalStoragehook so components using it (currentlyGoalTracker) stay in sync across browser tabs. Previously the hook only read fromlocalStorageonce on mount, so a change made in one tab (e.g. editing a goal) wasn't reflected in another open tab without a manual reload.This mirrors the existing cross-tab sync pattern already used in
context/TranslationContext.tsxfor language preference — just applied generically to the reusable hook.Changes:
storageevent listener inside the existing mountuseEffect, filtered to only react to the hook's own keyremoveEventListenerPillar
Visual Preview
N/A — this is a hook-level logic fix with no visual/UI change. Behavior is covered by unit tests; can be manually verified by opening the dashboard in two tabs and editing a goal in one.
Checklist before requesting a review:
CONTRIBUTING.mdfile.localhost:3000/api/streak?user=YOUR_USERNAME).npm run formatandnpm run lintlocally and resolved all errors (CI will fail otherwise).feat(themes): ...,fix(calculate): ...).README.mdif I added a new theme or URL parameter.