Skip to content

Function: useInjection()

Call Signature

ts
function useInjection<T>(token: ServiceToken<T>, options?: {
  fallback?: undefined;
  optional?: false;
}): T;

Defined in: src/wirestate-react/injection/use-injection.ts:38

Resolves a required value from the active container.

Type Parameters

Type ParameterDescription
TThe resolved value type.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options?{ fallback?: undefined; optional?: false; }Required lookup options.
options.fallback?undefinedMust be omitted.
options.optional?falseMust be omitted or false.

Returns

T

The resolved value.

Remarks

Use this overload when the token must be bound. The value re-resolves when the active container or token changes.

Throws

WirestateError when the token is not bound. Resolution errors from the container propagate unchanged.

Example

tsx
const api = useInjection(ApiService);

Call Signature

ts
function useInjection<T>(token: ServiceToken<T>, options: {
  fallback?: undefined;
  optional: true;
}): Optional<T>;

Defined in: src/wirestate-react/injection/use-injection.ts:64

Resolves an optional value from the active container.

Type Parameters

Type ParameterDescription
TThe resolved value type.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options{ fallback?: undefined; optional: true; }Optional lookup options.
options.fallback?undefinedMust be omitted.
options.optionaltrueMust be true.

Returns

Optional<T>

The resolved value, or undefined when the token is not bound.

Remarks

Use this overload when the token may be absent. A bound token is resolved normally. An unbound token returns undefined. The value re-resolves when the active container or token changes.

Throws

WirestateError when resolving a bound token fails. Resolution errors from the container propagate unchanged.

Example

tsx
const logger = useInjection(Logger, { optional: true });

Call Signature

ts
function useInjection<T, F>(token: ServiceToken<T>, options: {
  fallback: InjectionFallback<F>;
  optional?: boolean;
}): T | F;

Defined in: src/wirestate-react/injection/use-injection.ts:93

Resolves a value from the active container, or returns a fallback when the token is not bound.

Type Parameters

Type ParameterDescription
TThe resolved value type.
FThe fallback value type.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve.
options{ fallback: InjectionFallback<F>; optional?: boolean; }Fallback lookup options.
options.fallbackInjectionFallback<F>Value or factory used when the token is not bound.
options.optional?booleanOptional flag for compatibility with shared option objects.

Returns

T | F

The resolved value, or the fallback when the token is not bound.

Remarks

Providing fallback makes the lookup optional. The fallback may be a raw value or a factory that receives the active container. The value re-resolves when the active container or token changes. The fallback itself is not a memo dependency.

Throws

WirestateError when resolving a bound token fails. Resolution errors from the container propagate unchanged.

Example

tsx
const name = useInjection(UserName, { fallback: "guest" });
const logger = useInjection(FileLogger, { fallback: (container) => container.get(ConsoleLogger) });