For AI agents: the complete documentation index is available at https://navios.tech/llms.txt, the full documentation bundle is available at https://navios.tech/llms-full.txt, and this page is available as Markdown at https://navios.tech/docs/di/reference/react-api.md.

React API

Every public export of @navios/di-react, with signatures. This page is for looking things up; the React guides explain how the pieces are used. Peer dependencies: @navios/di and react ^18.0.0 || ^19.0.0.

ContainerProvider

<ContainerProvider container={container}>{children}</ContainerProvider>
ContainerProviderPropsTypeDescription
containerContainerThe root container. Must be the same object on every render
childrenReactNodeThe tree that can use the hooks

Every hook on this page throws when there is no ContainerProvider above it, with a message such as useContainer must be used within a ContainerProvider, except the scope hooks that return null or undefined.

ScopeProvider

<ScopeProvider scopeId="row-1" metadata={{ userId: '1' }}>{children}</ScopeProvider>
ScopeProviderPropsTypeDefaultDescription
scopeIdstringReact useId()Id of the scope, returned by useScope()
metadataRecord<string, unknown>nonePassed to container.createScope(). The key scopeId is overwritten with the scope id
childrenReactNodeThe tree that resolves from the scope

Creates one ScopedContainer from the root container on mount and calls its dispose() on unmount. scopeId and metadata are read on mount only. Nested providers create independent scopes. Throws ScopeProvider must be used within a ContainerProvider without one. See Scopes and invalidation.

Service hooks

All three hooks share one set of overloads. Result stands for the hook's return type.

// A class registered with @Injectable or @Factory
useService(token: ClassType): Result<Instance>

// A token with a schema: arguments are required
useService<T, S>(token: InjectionToken<T, S>, args: InferInput<S>): Result<T>

// A token without a schema, a bound token, a factory token
useService<T>(token: InjectionToken<T, undefined>): Result<T>
useService<T>(token: BoundInjectionToken<T, any>): Result<T>
useService<T>(token: FactoryInjectionToken<T, any>): Result<T>

For a class decorated with @Factory, Instance is the type its create() returns. Calling a hook with a schema token and no arguments gives it the return type MissingArgsError<S> from @navios/di, a string literal naming the missing keys, so the first use of the result fails to compile.

Each hook resolves from the nearest ScopeProvider's scope, or from the root container when there is none, and resolves again when the instance is destroyed.

useService

function useService(token, args?): UseServiceResult<T>

interface UseServiceResult<T> {
  data: T | undefined
  error: Error | undefined
  isLoading: boolean
  isSuccess: boolean
  isError: boolean
  refetch: () => void
}
FieldDescription
dataThe instance, once resolved
errorWhat container.get() rejected with, normally a DIError
isLoadingA resolution is in progress. false on the first render, before it starts
isSuccessdata is set. true on the first render when the container already holds the instance
isErrorerror is set
refetchCalls container.get() again. Returns the existing instance when there is one

args is compared by its JSON form; a new object with the same content does not restart the resolution. See Using services in components.

useSuspenseService

function useSuspenseService(token, args?): T

Returns the instance. Throws a promise while it is being resolved and the error when resolution fails, so it needs a Suspense boundary and an error boundary above it. Results are cached per container, token and JSON.stringify(args); a failure stays cached. See Suspense.

useOptionalService

function useOptionalService(token, args?): UseOptionalServiceResult<T>

interface UseOptionalServiceResult<T> {
  data: T | undefined
  error: Error | undefined
  isLoading: boolean
  isSuccess: boolean
  isNotFound: boolean
  isError: boolean
  refetch: () => void
}

The fields of UseServiceResult plus isNotFound. It is true, and error is undefined, when resolution fails because a provider is missing. Other failures are reported as in useService. See Optional services.

useInvalidateInstance

function useInvalidateInstance(): (instance: unknown) => Promise<void>

Returns a stable function that calls invalidate(instance) on the current container (the scope inside a ScopeProvider). The promise resolves when the instance and its dependents are destroyed. A value the container does not hold is ignored. See Invalidation.

Container hooks

HookReturnsDescription
useContainerContainer | ScopedContainerThe scope when inside a ScopeProvider, otherwise the root container
useRootContainerContainerThe root container, also inside a ScopeProvider

Scope hooks

HookReturnsOutside a ScopeProvider
useScopestring | nullnull
useScopeOrThrowstringthrows
useScopedContainerScopedContainer | nullnull
useScopedContainerOrThrowScopedContainerthrows
useScopeMetadata<T>(key)T | undefinedundefined

useScope returns the scope id. useScopeMetadata returns scope.getMetadata(key) cast to T, and undefined for a key that was not set. All of them read the innermost scope.

Contexts

const ContainerContext: React.Context<Container | null>
const ScopedContainerContext: React.Context<ScopedContainer | null>

The two contexts behind the providers and hooks. They are exported for code that has to read or supply them directly, for example a test helper; application code uses the providers.

Types

ExportDescription
ContainerProviderPropsProps of ContainerProvider
ScopeProviderPropsProps of ScopeProvider
UseServiceResult<T>Return type of useService
UseOptionalServiceResult<T>Return type of useOptionalService
UnionToArray<T>, Join<T, Delimiter>Type-level helpers. No hook signature uses them