UNPKG

@dr.pogodin/react-global-state

Version:
49 lines (48 loc) 2.53 kB
import { type ReactNode } from 'react'; import GlobalState from './GlobalState'; import type SsrContext from './SsrContext'; import type { ValueOrInitializerT } from './utils'; /** * 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 declare function useGlobalStateObject<StateT, SsrContextT extends SsrContext<StateT> = SsrContext<StateT>>(): GlobalState<StateT, SsrContextT>; /** * 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 declare function useSsrContext<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>> = (NewStateProps<StateT, SsrContextT> | { stateProxy: GlobalState<StateT, SsrContextT> | true; }) & { children?: ReactNode; }; /** * 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;