Function: useInjection()
Call Signature
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 Parameter | Description |
|---|---|
T | The resolved value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to resolve. |
options? | { fallback?: undefined; optional?: false; } | Required lookup options. |
options.fallback? | undefined | Must be omitted. |
options.optional? | false | Must 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
const api = useInjection(ApiService);Call Signature
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 Parameter | Description |
|---|---|
T | The resolved value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to resolve. |
options | { fallback?: undefined; optional: true; } | Optional lookup options. |
options.fallback? | undefined | Must be omitted. |
options.optional | true | Must 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
const logger = useInjection(Logger, { optional: true });Call Signature
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 Parameter | Description |
|---|---|
T | The resolved value type. |
F | The fallback value type. |
Parameters
| Parameter | Type | Description |
|---|---|---|
token | ServiceToken<T> | Token to resolve. |
options | { fallback: InjectionFallback<F>; optional?: boolean; } | Fallback lookup options. |
options.fallback | InjectionFallback<F> | Value or factory used when the token is not bound. |
options.optional? | boolean | Optional 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
const name = useInjection(UserName, { fallback: "guest" });
const logger = useInjection(FileLogger, { fallback: (container) => container.get(ConsoleLogger) });