redi (pronounced 'ready') is a lightweight dependency injection library for TypeScript and JavaScript, with React bindings included.
| Feature | Description |
|---|---|
| ๐ชถ Lightweight | Zero dependencies, small bundle size |
| ๐ง esbuild friendly | No emitDecoratorMetadata required |
| ๐ฆ Feature-rich | Class, value, factory, async injection and more |
| โ๏ธ React ready | Built-in hooks for React integration |
| โ Well tested | 100% code coverage |
npm install @wendellhu/rediimport { Inject, Injector } from '@wendellhu/redi';
class AuthService {
getCurrentUserInfo(): UserInfo {
/* ... */
}
}
class FileListService {
constructor(@Inject(AuthService) private authService: AuthService) {}
getUserFiles(): Promise<Files> {
const user = this.authService.getCurrentUserInfo();
// ...
}
}
const injector = new Injector([[AuthService], [FileListService]]);
const fileList = injector.get(FileListService);- Dependency Items: Class
{ useClass }, Value{ useValue }, Factory{ useFactory }, Async{ useAsync } - Interface Injection: Use
createIdentifierfor interface-based injection - Lazy Instantiation: Defer creation with
{ lazy: true } - Hierarchy Injection: Parent-child injectors with
@Self()and@SkipSelf() - Optional & Many:
@Optional()and@Many()decorators - React Integration:
useDependency,connectDependenciesand more hooks - RxJS Support:
useObservableanduseUpdateBinderfor reactive programming
- Univer
- ByteDance
MIT. Copyright 2021-present Wenzhao Hu.