Quick start
@navios/di-react connects a @navios/di container to a React tree. A provider puts the container in context, and hooks resolve services from it, re-render when a service is invalidated, and create scopes that live as long as a component. This page continues from the DI quick start and uses the DatabaseService and UserService declared there.
Install
React 18 and 19 are supported. The services are compiled by your bundler, so it has to handle standard decorators the same way the DI quick start asks of tsconfig.json: experimentalDecorators stays off.
Provide the container
Create one container for the application, outside any component, and hand it to ContainerProvider.
The container holds every singleton, so it must be the same object on every render. If it has to be created inside a component, keep it in useState(() => new Container()).
Everything a container accepts works here unchanged. To replace a dependency for a test or a Storybook story, pass providers to the container you give to ContainerProvider.
Use a service
useService resolves a service and returns it with its loading state.
users is typed as UserService | undefined. It is undefined until the instance is ready: the hook resolves with the container's asynchronous get(), because a service or one of its dependencies may have an asynchronous onServiceInit. When the instance already exists, the first render returns it with no loading state.
Every component that asks for UserService gets the same instance, the one the container holds. UserService injects DatabaseService on its own; components never see the dependency graph.
Or suspend
useSuspenseService returns the instance itself and lets a Suspense boundary show the loading state.
Related: Using services in components, Scopes and invalidation, React API.