0Pricing
React Academy · Lesson

Handling Storage Events Across Tabs

React to localStorage changes in other browser tabs using the storage event listener.

Handling Storage Events Across Tabs is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Cross-Tab Staleness Problem

When a user has your app open in two tabs and updates a value in tab A, tab B still shows the old state. Each tab has its own React state in memory even though they share localStorage.

Without coordination, the tabs disagree about the current value.

The storage Event Fires in Other Tabs

The browser dispatches a storage event on other tabs of the same origin whenever localStorage changes. Importantly, it does not fire in the tab that made the change.

This is exactly the signal tab B needs to learn that tab A updated something.

Listening with addEventListener

In a useEffect you call window.addEventListener('storage', handler) and return a cleanup that removes it. This subscribes the component to cross-tab changes.

The effect should run once on mount, so an empty or stable dependency array is appropriate.

StorageEvent Properties

The event object carries key (which key changed), oldValue and newValue (the strings before and after), and storageArea (the affected storage object).

A null key with null values means the storage was cleared entirely.

Filtering to Your Keys

The storage event fires for every key in the origin, so your handler should check event.key === yourKey before reacting. Ignoring unrelated keys avoids needless re-renders.

Also handle the case where event.newValue is null, meaning the key was removed.

Updating React State on the Event

When a relevant change arrives, parse event.newValue and call your setter to update React state. This brings the second tab in sync with the change made elsewhere.

Combined with a useLocalStorage hook, this closes the loop between tabs.

The BroadcastChannel Alternative

BroadcastChannel lets tabs of the same origin send messages to each other directly with postMessage. It is cleaner than piggybacking on storage events and works even without writing to localStorage.

You create a channel by name, post updates, and listen for message events.

useSyncExternalStore for Storage

React provides useSyncExternalStore for subscribing to external stores in a concurrent-safe way. You can wire its subscribe function to the storage event and its snapshot to a localStorage read.

This integrates external storage updates correctly with React rendering.

Logout Across Tabs Use Case

A classic application is logging out everywhere: when one tab clears the auth flag, the storage event in other tabs detects it and redirects them to the login screen.

This prevents a stale tab from continuing to show authenticated content.

Syncing Dark Mode Across Tabs

Theme is another great fit: toggling dark mode in one tab writes to localStorage, the storage event fires in the others, and they update their theme state to match instantly.

The user gets a consistent appearance no matter which tab they switch to.

Putting It Together

Cross-tab sync comes down to listening for the storage event (or using BroadcastChannel), filtering to your keys, and updating React state from the new value.

For robust, concurrent-safe subscriptions, useSyncExternalStore is the modern choice.

Quick Check

Test your understanding of cross-tab events.

Recap

You learned that the storage event fires in other tabs, how to listen, filter by key, and update state, plus the BroadcastChannel and useSyncExternalStore alternatives.

You saw real use cases like logout-everywhere and cross-tab dark mode.

Frequently asked questions

Is the “Handling Storage Events Across Tabs” lesson free?

Yes — the full text of “Handling Storage Events Across Tabs” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Handling Storage Events Across Tabs”?

React to localStorage changes in other browser tabs using the storage event listener. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Handling Storage Events Across Tabs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading and Writing localStorage in React
  2. Syncing State with localStorage
  3. Handling Storage Events Across Tabs
  4. sessionStorage, IndexedDB, and Choosing the Right API
← Back to React Academy