Skip to main content

React Hooks

@brushy/di provides React hooks to facilitate dependency injection in functional components.

useInject

The useInject hook allows injecting dependencies into React components.

Import

Basic Usage

Options

Promise Caching

By default, useInject creates a proxy around the injected service that automatically caches promises returned by methods. This is useful to avoid multiple API calls during re-renders.

useInjectLazy

The useInjectLazy hook resolves a dependency lazily via a proxy - the service is created on first property or method access.

Import

Basic Usage

Options

Provider Integration

For the hooks to work, you need to wrap your application with BrushyDIProvider:

Complete Example