React Component Injection
@brushy/di supports component injection: register UI by token on the same Container used for services, then resolve with useInjectComponent in your shell (themes, white-label, A/B).
Tokens: Symbol only (never strings)
UsecreateToken("…") or Symbol("…"). Never use plain strings. At runtime, createToken returns Symbol(description); each token is unique and avoids collisions between modules or libraries.
createToken over raw Symbol so container.register and useInjectComponent infer types from the registered component.
Recommended: register on the Container
Register React components like any other provider withuseValue on the container. No separate registration API is required.
Declarative bootstrap
Imperative registration
container.register returns a typed token. Props flow to useInjectComponent without extra generics:
Type inference (no manual generics required)
You do not needcreateToken<React.ComponentType<ButtonProps>>("BUTTON") in most cases. When you register with useValue, TypeScript infers the component type from the implementation. This is the same pattern used in @brushy/di-react tests:
tokens.ts consumed by multiple theme packages).
useInjectComponent
Resolves a component from the nearestBrushyDIProvider container.
Import
With fallback
setInjectComponentErrorRenderer once at bootstrap. See Getting Started.
Optional helpers
These wrapcontainer.register. Prefer the container API above for consistency with the rest of your DI graph.
Complete example: extensible UI shell
providers or container.import a child container. See Best Practices.
Server Components
- Register components on the container during bootstrap (server or client).
useInjectComponentruns in Client Components; pair with@brushy/di/coreon the server for services.