fn
useContextFactory
v0.0.1testeddemoA 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
ContextValueParameters
| Parameter | Type | Description |
|---|---|---|
name | string | The 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