Skip to content

Function: useInjection()

Call Signature

ts
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, token: ServiceToken<T>): UseInjectionValue<T>;

Defined in: src/wirestate-lit/consumption/use-injection.ts:102

Consumes a value from the nearest Lit container context.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
hostReactiveControllerHost & HTMLElementHost element.
tokenServiceToken<T>Token to inject, or options carrying the token plus value/optional/fallback.

Returns

UseInjectionValue<T>

Injection holder.

Remarks

Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.

Example

typescript
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });

Call Signature

ts
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, options: UseInjectionOptions<T, undefined> & {
  fallback?: undefined;
  optional?: false;
}): UseInjectionValue<T>;

Defined in: src/wirestate-lit/consumption/use-injection.ts:106

Consumes a value from the nearest Lit container context.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
hostReactiveControllerHost & HTMLElementHost element.
optionsUseInjectionOptions<T, undefined> & { fallback?: undefined; optional?: false; }-

Returns

UseInjectionValue<T>

Injection holder.

Remarks

Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.

Example

typescript
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });

Call Signature

ts
function useInjection<T>(host: ReactiveControllerHost & HTMLElement, options: UseInjectionOptions<T, undefined> & {
  fallback?: undefined;
  optional: true;
}): UseInjectionValue<T, undefined>;

Defined in: src/wirestate-lit/consumption/use-injection.ts:110

Consumes a value from the nearest Lit container context.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
hostReactiveControllerHost & HTMLElementHost element.
optionsUseInjectionOptions<T, undefined> & { fallback?: undefined; optional: true; }-

Returns

UseInjectionValue<T, undefined>

Injection holder.

Remarks

Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.

Example

typescript
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });

Call Signature

ts
function useInjection<T, F>(host: ReactiveControllerHost & HTMLElement, options: UseInjectionOptions<T, F> & {
  fallback: InjectionFallback<F>;
}): UseInjectionValue<T, F>;

Defined in: src/wirestate-lit/consumption/use-injection.ts:114

Consumes a value from the nearest Lit container context.

Type Parameters

Type Parameter
T
F

Parameters

ParameterTypeDescription
hostReactiveControllerHost & HTMLElementHost element.
optionsUseInjectionOptions<T, F> & { fallback: InjectionFallback<F>; }-

Returns

UseInjectionValue<T, F>

Injection holder.

Remarks

Returns a holder whose value updates when the container context changes. Throws when the token is not bound. Pass optional to hold undefined on a miss, or a fallback (which implies optional) to hold a default. For a required lookup, reading value before the context resolves (no container provider above the host) throws, unless an initial value is set.

Example

typescript
private service = useInjection(this, MyService);
private maybe = useInjection(this, { token: MyService, optional: true });
private name = useInjection(this, { token: UserName, fallback: "guest" });