@dr.pogodin/react-global-state
Version:
Hook-based global state for React
102 lines (100 loc) • 3.27 kB
JavaScript
import { c as _c } from "react/compiler-runtime";
import { createContext, use, useState } from 'react';
import GlobalState from "./GlobalState.js";
import { jsx as _jsx } from "react/jsx-runtime";
const Context = /*#__PURE__*/createContext(null);
/**
* Returns the GlobalState object from the context. In most cases you should use
* other hooks, like useGlobalState(), etc. to interact with the global state,
* instead of accessing the GlobalState object directly.
*/
export function useGlobalStateObject() {
const globalState = use(Context);
if (!globalState) throw new Error('Missing GlobalStateProvider');
return globalState;
}
/**
* Returns SSR context.
* @param throwWithoutSsrContext - If _true_ (default), this hook will throw
* if no SSR context is attached to the global state; set _false_ to not throw
* in such case. In either case this hook will throw if the <GlobalStateProvider>
* (hence the global state) is missing.
* @returns SSR context.
* @throws
* - If current component has no parent <GlobalStateProvider> in the rendered
* React tree.
* - If `throwWithoutSsrContext` is _true_ (default), and there is no SSR
* context attached to the global state provided by <GlobalStateProvider>.
*/
export function useSsrContext(t0) {
const throwWithoutSsrContext = t0 === undefined ? true : t0;
const {
ssrContext
} = useGlobalStateObject();
if (!ssrContext && throwWithoutSsrContext) {
throw new Error("No SSR context found");
}
return ssrContext;
}
/**
* Provides global state to its children.
* @param prop.children Component children, which will be provided with
* the global state, and rendered in place of the provider.
* @param prop.initialState Initial content of the global state.
* @param prop.ssrContext Server-side rendering (SSR) context.
* @param prop.stateProxy This option is useful for code
* splitting and SSR implementation:
* - If `true`, this provider instance will fetch and reuse the global state
* from a parent provider.
* - If `GlobalState` instance, it will be used by this provider.
* - If not given, a new `GlobalState` instance will be created and used.
*/
const GlobalStateProvider = t0 => {
const $ = _c(3);
const {
children,
...rest
} = t0;
const [localState, setLocalState] = useState();
let state;
if ("stateProxy" in rest && rest.stateProxy) {
if (localState) {
setLocalState(undefined);
}
if (rest.stateProxy === true) {
const gs = use(Context);
if (!gs) {
throw Error("Missing GlobalStateProvider");
}
state = gs;
} else {
state = rest.stateProxy;
}
} else {
if (localState) {
state = localState;
} else {
const {
initialState,
ssrContext
} = rest;
state = new GlobalState(typeof initialState === "function" ? initialState() : initialState, ssrContext);
setLocalState(state);
}
}
let t1;
if ($[0] !== children || $[1] !== state) {
t1 = /*#__PURE__*/_jsx(Context, {
value: state,
children: children
});
$[0] = children;
$[1] = state;
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
};
export default GlobalStateProvider;
//# sourceMappingURL=GlobalStateProvider.js.map