R
fn

useContextFactory

v0.0.1testeddemo

A composable that provides a factory for creating context with unique key

Examples

ts
const { inject, provide } = useContextFactory('MyContext');

provide('Hello World');
const value = inject();
ts
const { inject: injectContext, appProvide } = useContextFactory('MyContext');

// In a plugin
{
  install(app) {
     appProvide(app)('Hello World');
  }
}

// In a component
const value = injectContext();

Demo

Loading demo…

Signature

ts
export function useContextFactory<ContextValue>(name: string){ ... }

Type Parameters

ContextValue

Parameters

ParameterTypeDescription
namestringThe name of the context

Returns

{ inject: <Fallback extends ContextValue = ContextValue>(fallback?: Fallback) => ContextValue & ({} | null); provide: (context: ContextValue) => ContextValue; appProvide: (app: App) => (context: ContextValue) => ContextValue; key: InjectionKey<ContextValue>; }An object with `inject`, `provide`, `appProvide` and `key` properties