UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

23 lines (22 loc) • 904 B
'use client'; import { createContext, createElement, useContext, useRef } from 'react'; import { createStore, getDefaultStore } from '../vanilla.js'; const StoreContext = createContext(undefined); export function useStore(options) { const store = useContext(StoreContext); return options?.store || store || getDefaultStore(); } export function Provider({ children, store, }) { const storeRef = useRef(null); if (store) { return createElement(StoreContext.Provider, { value: store }, children); } if (storeRef.current === null) { storeRef.current = createStore(); } return createElement(StoreContext.Provider, { // TODO: If this is not a false positive, consider using useState instead of useRef like https://github.com/pmndrs/jotai/pull/2771 // eslint-disable-next-line react-hooks/refs value: storeRef.current, }, children); }