What is use-sync-external-store?
The use-sync-external-store package provides a hook that allows React components to read from an external, synchronous data source and subscribe to updates from that source. It is designed to be a primitive hook for creating other hooks that need to subscribe to external data sources.
Subscribing to an external store
This feature allows you to create a custom hook that subscribes to an external store. The hook will re-render the component whenever the store updates.
import { useSyncExternalStore } from 'use-sync-external-store';
function useCustomHook(store) {
const state = useSyncExternalStore(
store.subscribe,
store.getSnapshot,
store.getServerSnapshot
);
return state;
}