Skip to main content
Version: 0.1.2

Variable: useInjector

Imported from @react-logic/di

const useInjector: UseInjectorFn

Resolves a dependency from the surrounding <Injector> scope, scoped to the React subtree where it's called.

Use this when a React component (not a logic class) needs to read a service directly. Inside logic classes, prefer inject() — it's synchronous, doesn't need a hook, and is what <useLogic> is designed to orchestrate.

The hook does not subscribe to the resolved value's signals. If you need the component to re-render when the service's state changes, call the relevant signal directly inside the render body (e.g. svc.count()) — the hook order around it is irrelevant.

Type Param

The type the token resolves to. With optional: true the return widens to T | null.

Example

// Required injection — throws if no provider is found.
const myService = useInjector(MyService);

// Optional — returns null if no provider is found.
const maybeService = useInjector(MyService, true);