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

Taught in

References