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).
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>();
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
- Prior to v0.24.0 it was named
getSsrContext(); renamed to avoid eslint-plugin-react-hooks complains about its implementation. - Prior to v0.18.0 it was a true hook, based on the React's useContext(),
thus having to respect the rules of hooks to a letter; since v0.18.0 it is
based on use() instead, thus can be called within loops and conditional
statements (like
if).
Generic Parameters
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> object, or undefined
(if throwWithoutSsrContext set false, and there is no SsrContext).
This function throws in these cases:
- If current component has no parent GlobalStateProvider in the rendered React tree.
- If
throwWithoutSsrContextis true, and there is no SSR context attached to the global state provided by GlobalStateProvider.