useSyncExternalStore
The React hook for reading data from a store outside React, such as a state library or a browser API, without tearing.
In more detail
You give it a subscribe function, a getSnapshot function that returns the current value, and optionally getServerSnapshot for server rendering. getSnapshot must return the same value if nothing changed, or React will re-render forever. React re-reads the store synchronously when it changes, so all components show the same snapshot.
Example
useSyncExternalStore(store.subscribe, store.getSnapshot)Related terms
- external store — State held outside React, in a library, a module variable or a browser API, which components subscribe to for changes.
- tearing — A visual glitch in which different parts of the screen show different versions of the same data at the same time.
- concurrent rendering — React ability to prepare several versions of the UI at once and to pause, resume or abandon rendering work so urgent updates are handled first.
- server-side rendering — Building a page’s HTML on the server for each request, so the browser can show content before any JavaScript runs.
Taught in
- Custom Hook Composition: Encapsulating Reactive Logic — React 19 & Modern Component Architecture
- useSyncExternalStore: Tearing-Free Store Subscriptions — React 19 & Modern Component Architecture