Skip to main content

useGlobalStateObject()

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

function useGlobalStateObject<StateT>(): GlobalState<StateT>;

Gets a GlobalState instance from the context.

caution

In most cases you should use useGlobalState() and other hooks to interact with the global state, instead of accesing the GlobalState object directly.

tip

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

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

const { useGlobalStateObject } = 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 global state type.

Result

Returns GlobalState<StateT> instance.