Vize

create-global-state

Lift a state factory into application-wide state shared by every caller.

Package @vizejs/composable/create-global-state
Own the source vize lib pull composable:create-global-state
Runtime exports createGlobalState
Gzip budget 1024 B

Usage

import { createGlobalState } from "@vizejs/composable/create-global-state";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
createGlobalState state experimental safe caller-managed none web, server, worker, native, desktop, terminal — —

API

createGlobalState

Lift a state factory into application-wide state shared by every caller. The factory runs once, lazily on the first call, inside a detached effect scope so its watchers and computeds outlive any component; every later call returns the same state. Because the state is module-level, it is shared across all server requests: on the server, keep per-request state in provide/createInjectionState instead, or reset it per request.

function createGlobalState<State>(factory: () => State): () => State
const useSession = createGlobalState(() => ({ user: shallowRef<User | null>(null) }));
useSession().user.value = currentUser;