@react-logic/di
v0.1.2
Published
Lightweight dependency injection for React — `Injector`, `inject`, `InjectionToken`, `onDestroy`.
Readme
@react-logic/di
Lightweight dependency injection for React — Injector, inject, InjectionToken, onDestroy.
Part of react-logic. For the full toolkit in one install, use @react-logic/react-logic.
Install
npm install @react-logic/diPeer dependency: react@^18 || ^19.
Usage
import { Injector, inject } from '@react-logic/di';
class ApiService {
fetchUsers() { return fetch('/users').then(r => r.json()); }
}
class UsersLogic {
api = inject(ApiService);
load() { return this.api.fetchUsers(); }
}
export function App() {
return (
<Injector providers={[ApiService]}>
<UsersList />
</Injector>
);
}- Auto-resolves class providers without manual registration.
- Override with
{ provide: Token, useClass: ... }/useValue/useFactory. onDestroy(fn)registers cleanup tied to the surroundingInjector(or logic instance).
See the project README for full docs and demos.
License
MIT
