UNPKG

@dr.pogodin/react-global-state

Version:
51 lines (50 loc) 2.55 kB
import { type ReactNode } from 'react'; import GlobalState from './GlobalState'; import type SsrContext from './SsrContext'; import type { ValueOrInitializerT } from './utils'; /** * Gets {@link GlobalState} instance from the context. In most cases * you should use {@link useGlobalState}, and other hooks to interact with * the global state, instead of accessing it directly. * @return */ export declare function getGlobalState<StateT, SsrContextT extends SsrContext<StateT> = SsrContext<StateT>>(): GlobalState<StateT, SsrContextT>; /** * @category Hooks * @desc Gets 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 the hook will throw * if the {@link &lt;GlobalStateProvider&gt;} (hence the state) is missing. * @returns SSR context. * @throws * - If current component has no parent {@link &lt;GlobalStateProvider&gt;} * in the rendered React tree. * - If `throwWithoutSsrContext` is `true`, and there is no SSR context attached * to the global state provided by {@link &lt;GlobalStateProvider&gt;}. */ export declare function getSsrContext<SsrContextT extends SsrContext<unknown>>(throwWithoutSsrContext?: boolean): SsrContextT | undefined; type NewStateProps<StateT, SsrContextT extends SsrContext<StateT>> = { initialState: ValueOrInitializerT<StateT>; ssrContext?: SsrContextT; }; type GlobalStateProviderProps<StateT, SsrContextT extends SsrContext<StateT>> = { children?: ReactNode; } & (NewStateProps<StateT, SsrContextT> | { stateProxy: true | GlobalState<StateT, SsrContextT>; }); /** * 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. */ declare const GlobalStateProvider: <StateT, SsrContextT extends SsrContext<StateT> = SsrContext<StateT>>({ children, ...rest }: GlobalStateProviderProps<StateT, SsrContextT>) => ReactNode; export default GlobalStateProvider;