forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCrossTabStorage.ts
More file actions
28 lines (25 loc) · 988 Bytes
/
Copy pathuseCrossTabStorage.ts
File metadata and controls
28 lines (25 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"use client";
import { useEffect } from "react";
/**
* Subscribes to the native `storage` event so that a `localStorage` write
* made in one tab is reflected in every other same-origin tab. The browser
* only fires this event in tabs *other* than the one that made the write,
* so this is purely additive reconciliation — it never fires for, and
* never needs to guard against, the tab's own same-tab state updates (#84).
*
* `newValue` is `null` when the key was removed (e.g. logout/disconnect)
* and the new string value otherwise (e.g. login/connect from another tab).
*/
export function useCrossTabStorage(
key: string,
onChange: (newValue: string | null) => void,
) {
useEffect(() => {
function handleStorage(event: StorageEvent) {
if (event.key !== key) return;
onChange(event.newValue);
}
window.addEventListener("storage", handleStorage);
return () => window.removeEventListener("storage", handleStorage);
}, [key, onChange]);
}