GlobalState
A GlobalState object is an instance of the GlobalState class, which holds and manages the global state content. Users are not supposed to create new GlobalState instances explicitly, but they may access the current instance via the getGlobalState() hook. It allows imperative interactions with the global state, instead of the normal declarative state access via the useGlobalState() hook.
Methods
- get() - Reads a value from the GlobalState.
- set() - Writes a value into the GlobalState.
- unWatch() - Unsubscribes a listener from global state update notifications.
- watch() - Subscribes a listener for global state update notifications.
get()
globalState.get(path, options): any
Gets current or initial value at the specified path
of the global state.
It allows to get the entire global state, and automatically sets default value
at the path
.
path
- string - Optional. Dot-delimitered state path. If null or undefined the entire global state is returned.options
- object - Optional. Additional parameters.initialState
- boolean - If true the value will be read from the initial global state (at the moment of the GlobalState construction), instead of the current global state.initialValue
- any - Optional. Intended initial value atpath
. In case undefined value was read from the statepath
(either from the current, or initial state, depending on theinitialState
flag), thisinitialValue
will be returned to the caller instead.initialValue
will be also written to thepath
of the current state, provided the current value there is undefined.
- Returns any - The result value.
set()
globalState.set(path, value): any
Writes the value
to given global state path
.
path
- string - Optional. Dot-delimitered state path. Without it thevalue
will replace the entire global state content.value
- any - The value to set.- Returns any - The given
value
.
unWatch()
globalState.unWatch(callback)
Unsubscribes callback
from the state update notifications; no operation if
callback
is not subscribed.
callback
- function - The callback to unsubscribe.
This method throws if SsrContext is attached to the state instance: the state watching functionality is intended for the client side only.
watch()
globalState.watch(callback)
Subscribes callback
to watch state updates; no operation if callback
is already subscribed to this state instance.
callback
- function - It will be triggered without any arguments each time the state context changes (however, the library may combine several independent state updates and call thiscallback
just once when all these updatesare completed).
This method throws if SsrContext is attached to the state instance: the state watching functionlity is intended for the client-side only.