Skip to main content

useSsrContext()

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

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

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

tip

You may use withGlobalStateType() function to get useSsrContext() variant with a "locked-in" StateT:

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

const { useSsrContext } = withGlobalStateType<StateT>();
info

Technically, it only depends on use(), thus it is not a regular hook bound by the rules of hooks, and it can be called within loops and conditional statements (like if). However, as of now eslint-plugin-react-hooks does not make distinction between use() and regular hooks (functions with names prefixed by use), thus we prefer to name and treat it as a regular hook.

Changelog

Generic Parameters​

  • StateT — The type of global state content.

Arguments​

  • throwWithoutSsrContext — boolean — 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.