Problem, as an interviewer would state it: "A user changes a setting (like the theme) in one open tab. How would you make every OTHER open tab of the same app pick up that change automatically, using nothing but localStorage?"
Examples:
// tab A: localStorage.setItem("theme", "dark")
// tab B: window.addEventListener("storage", (e) => { if (e.key === "theme") applyTheme(e.newValue); })
Clarifying questions expected:
- Does the tab that MADE the change need to react to its own write too, or only the others?
- Is
BroadcastChannelan acceptable alternative, or does the solution specifically need to uselocalStorage? - Should this work across tabs that were already open before the change, newly opened tabs, or both?
Code / implementation expected: Yes — real, live proof of the storage event's own real, sharp self-exclusion behavior: it does not fire in the writing document, but does fire in other real, same-origin contexts.