useLocalStorage
A `localStorage`-backed `useState` built on `useSyncExternalStore`: no hydration mismatch under SSR, live updates when another tab writes, and live updates when another hook on the same page writes.
3 min read
#frontendlocalStorage is external mutable state that
#react doesn't track, which is exactly what
useSyncExternalStore is built for. Subscribe
through it and you get the three things a hand-rolled useState +
useEffect version usually drops: a server render that matches the
client's first paint, updates when another tab writes, and updates
when another instance of the hook on the same page writes.
@itsnas editor.tsx
codeeditor.tsx
main
Nas (@itsnas)
main
Nas (@itsnas)