channelstate
Version:
A lightweight cross-context state management library built on the BroadcastChannel API.
11 lines (8 loc) • 412 B
JavaScript
import { useDebugValue } from 'react';
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
function useChannelStateWithSelector(api, selector = (v) => v, equalityFn) {
const state = useSyncExternalStoreWithSelector(api.subscribe, api.getState, api.getInitialState, selector, equalityFn);
useDebugValue(state);
return state;
}
export { useChannelStateWithSelector };