UNPKG

@mui/x-internals

Version:

Utility functions for the MUI X packages (internal use only).

20 lines 1.28 kB
import * as React from 'react'; /* We need to import the shim because React 17 does not support the `useSyncExternalStore` API. * More info: https://github.com/mui/mui-x/issues/18303#issuecomment-2958392341 */ import { useSyncExternalStore } from 'use-sync-external-store/shim'; import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'; import reactMajor from "../reactMajor/index.js"; /* Some tests fail in R18 with the raw useSyncExternalStore. It may be possible to make it work * but for now we only enable it for R19+. */ const canUseRawUseSyncExternalStore = reactMajor >= 19; const useStoreImplementation = canUseRawUseSyncExternalStore ? useStoreR19 : useStoreLegacy; export function useStore(store, selector, a1, a2, a3) { return useStoreImplementation(store, selector, a1, a2, a3); } function useStoreR19(store, selector, a1, a2, a3) { const getSelection = React.useCallback(() => selector(store.getSnapshot(), a1, a2, a3), [store, selector, a1, a2, a3]); return useSyncExternalStore(store.subscribe, getSelection, getSelection); } function useStoreLegacy(store, selector, a1, a2, a3) { return useSyncExternalStoreWithSelector(store.subscribe, store.getSnapshot, store.getSnapshot, state => selector(state, a1, a2, a3)); }