Overview
JavaScript vs. TypeScript API
This API reference uses TypeScript vocabulary and syntax to describe both JavaScript and TypeScript flavors of the library. Most stuff in this library works the same way both in JS and TS, beside static type-checks, of course. Non-trivial differences between JS and TS flavors of the library will be explained explicitly where needed.
Classes
- GlobalState — An object that holds and manages the global state content.
- SsrContext — Holds data persistent across multiple SSR (server-side rendering) iterations.
Components
- GlobalStateProvider — Provides GlobalState to its children tree.
Functions
- newAsyncDataEnvelope — Creates a new plain JS object, satisfying the AsyncDataEnvelopeT type, and with reasonable initial values of its fields.
- withGlobalStateType — Returns a set of library components, classes, and hooks "locked-in" onto a specified state type. Thus, making it unnecessary (in TypeScript) to pass the state type as a generic parameter for each component, class, and hook, when they are used, and allowing TypeScript compiler to auto-evaluate their other generic parameters in many cases.
Environment Variables
- REACT_GLOBAL_STATE_DEBUG — Enables debug logging.
Hooks
- getGlobalState() — Gets GlobalState instance from the context.
- getSsrContext() — Gets SsrContext object (the server-side rendering context, which is used to pass global-state-related data between rendering iterations).
- useAsyncCollection() — Resolves and stores at the given path of global state elements of an asynchronous data collection.
- useAsyncData() — Resolves asynchronous data, and stores them at the given path of global state.
- useGlobalState() — The primary hook for interacting with the global state, modeled after the standard React's useState().
TypeScript Types
- AsyncCollectionLoaderT — The signature of a valid data loader function for useAsyncCollection() hook.
- AsyncCollectionT — The type of a global state segment managed by the useAsyncCollection() hook.
- AsyncDataEnvelopeT — The type of a state segment storing data managed by useAsyncData() and useAsyncCollection() hooks, alongside related meta-data.
- AsyncDataLoaderT — The signature of a valid data loader function for useAsyncData() hook.
- ForceT — A special type for «unlocking» overloads of hooks and functions that allow to enforce arbitrary value types.
- SetterT — The type of setter returned by the useGlobalState() hook.
- UseAsyncDataOptionsT — The type of options object accepted by useAsyncData() and useAsyncCollection() hooks.
- UseAsyncCollectionResT — The type of result possibly returned by useAsyncCollection() hooks (depends on hook arguments).
- UseAsyncDataResT — The type of result returned by useAsyncData() and, possibly, useAsyncCollection() hooks (depends on hook arguments).
- UseGlobalStateResT — The type of result returned by the useGlobalState() hook.
- ValueOrInitializerT — The type for arguments that accept either a value to use, or a function that returns the value to use.