Skip to content

useSyncExternalStore

Types

UseSyncExternalStoreProps

  • @template Snapshot - The type of the snapshot value returned by getSnapshot.

Parameters accepted by useSyncExternalStore.

PropertyTypeRequiredDescription
subscribe(onStoreChange: () => void) => () => voidA function that subscribes to the external store. Receives an onStoreChange callback that must be called whenever the store changes. Must return a cleanup function that unsubscribes when called. React may call this multiple times with different callbacks — always use the latest one.
getSnapshot() => SnapshotA function that returns the current snapshot of the store. Must be a pure function that returns the same value when the store has not changed, allowing React to bail out of re-renders via referential equality. Called on every render and after every store change notification.

UseSyncExternalStoreResult

  • @template Snapshot - The type of the snapshot value.

Return value of useSyncExternalStore.

The current snapshot of the external store, reactive — the component re-renders whenever subscribe notifies React of a change and getSnapshot returns a different value.

ts
export type UseSyncExternalStoreResult<Snapshot> = Snapshot;

Released under the MIT License.