Custom Effects Hooks: Demystifying useSyncExternalStore to Build Resilient, Framework-Agnostic State Listeners

If you've ever synced a React component to window.innerWidth, a WebSocket connection, localStorage, or a third-party state container, you've probably reached for the same combo: useEffect to subscribe, useState to hold the value, and a cleanup function to unsubscribe. It works — until it doesn't. Under React 18's concurrent rendering, that pattern can quietly produce tearing: different parts of your UI rendering with different, conflicting snapshots of the same external value at the same instant.

useSyncExternalStore is React's purpose-built answer to this problem. It's not a flashy hook, and it doesn't show up in beginner tutorials next to useState and useEffect. But if you're building custom hooks that read from anything outside React — browser APIs, global variables, third-party stores, WebSocket clients — it's the one hook engineered specifically to keep those subscriptions correct, consistent, and portable across rendering strategies.

This guide breaks down exactly what useSyncExternalStore does, why it exists, and how to use it to build genuinely framework-agnostic state listeners: hooks whose subscription logic could run unmodified in Preact, a vanilla JS class, or an entirely different UI layer, with React acting only as the final rendering step.