Vize

create-injection-state

Create a typed provide/inject pair around a composable, for component subtrees that share request-local state.

Package @vizejs/composable/create-injection-state
Own the source vize lib pull composable:create-injection-state
Runtime exports createInjectionState
Gzip budget 512 B

Usage

import { createInjectionState } from "@vizejs/composable/create-injection-state";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
createInjectionState state experimental safe stable none web, server, worker, native, desktop, terminal — —

API

createInjectionState

Create a typed provide/inject pair around a composable, for component subtrees that share request-local state. The provider runs the composable with its arguments, provides the result under a typed InjectionKey, and returns it. The injector returns the nearest provided state, or defaultValue, or undefined (reflected in its return type). State lives in the component tree, so unlike module globals it is naturally per request during SSR. Both must be called during setup (or inside app.runWithContext).

function createInjectionState<Arguments extends readonly unknown[], State>( composable: (...args: Arguments) => State, options: CreateInjectionStateOptions<State> & { readonly defaultValue: State }, ): InjectionState<Arguments, State, State>
const [useProvideCounter, useCounter] = createInjectionState((initial: number) => {
  const count = ref(initial);
  return { count, increment: () => count.value++ };
});

createInjectionState

function createInjectionState<Arguments extends readonly unknown[], State>( composable: (...args: Arguments) => State, options?: CreateInjectionStateOptions<State>, ): InjectionState<Arguments, State, State | undefined>

createInjectionState

function createInjectionState<Arguments extends readonly unknown[], State>( composable: (...args: Arguments) => State, options: CreateInjectionStateOptions<State> = {}, ): InjectionState<Arguments, State, State | undefined>

Types

CreateInjectionStateOptions

Options for createInjectionState.

Member Type Description
injectionKey? InjectionKey<State> Key used for provide/inject.
defaultValue? State Value injected when no ancestor provides the state; also narrows the injector's return type to State.