Skip to main content

getSsrContext()

import { getSsrContext } from '@dr.pogodin/react-global-state';

Gets SsrContext object (the server-side rendering context, which is used to pass global-state-related data between rendering iterations).

info

Prior to the library v0.18.0 this function was a hook, based on the React's useContext(), thus having to respect the rules of hooks. Starting from v0.18.0 it is based on use() instead, thus can be called within loops and conditional statements like if.

The TypeScript signature of this function is:

function getSsrContext<StateT>(
throwWithoutSsrContext = true,
): SsrContext<StateT> | undefined;
tip

Alternatively you may use withGlobalStateType() function to get getSsrContext() variant with a "locked-in" StateT:

import { withGlobalStateType } from '@dr.pogodin/react-global-state';

const { getSsrContext } = withGlobalStateType<StateT>();

Generic Parameters

  • StateT — The type of global state content.

Arguments

  • throwWithoutSsrContextboolean — If true (default) this hook will throw if no SSR context is attached to the global state. Pass in false to not throw in such case. In either case the hook will throw if no parent GlobalStateProvider (hence no global state) is found.

Result

Returns SsrContext<StateT&gt object, or undefined (if throwWithoutSsrContext set false, and there is no SsrContext).

caution

This function throws in these cases:

  • If current component has no parent GlobalStateProvider in the rendered React tree.
  • If throwWithoutSsrContext is true, and there is no SSR context attached to the global state provided by GlobalStateProvider.