redi
redi (pronounced ‘ready’) is a lightweight dependency injection library for TypeScript and JavaScript, with React bindings included.
Why redi?
| 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 |
Quick Start
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);Features
- 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
Who’s Using redi?
- Univer
- ByteDance
Links
License
MIT. Copyright 2021-present Wenzhao Hu.
Last updated on