0PricingLogin
React Academy · Lesson

The Problem with External Store Subscriptions

Understand tearing and why useState or useRef subscriptions to external stores are unsafe in concurrent React.

What an External Store Is

An external store is any source of state that lives outside the React tree, such as a Redux store, a browser API like matchMedia, or a plain module-level object with its own subscribe mechanism.

React does not own this state, so it must subscribe to changes and read snapshots to keep components in sync with whatever the store currently holds.

The Naive useState + useEffect Subscription

A common approach reads the store value into useState, then uses useEffect to subscribe and call setState whenever the store changes. On the surface this seems to keep the component current.

It works for simple synchronous rendering, but it has a subtle flaw that becomes visible once React renders concurrently and can pause or restart work mid-flight.

All lessons in this course

  1. The Problem with External Store Subscriptions
  2. useSyncExternalStore API and Parameters
  3. Subscribing to Browser APIs
  4. Building a Custom Store with useSyncExternalStore
← Back to React Academy