ReactDI
ReactDI is a simple DI container originaly built for React.js. It is a general solution that allows you to register arbitrary objects to be passed down component hierarchy.
To install, please run
npm install react-di
or via bower
bower install react-di
Usage
Basic setup
To setup ReactDI, you have to obtain an instance of ReactDI first.
// If you're using Node.js, Browserify, Webpack or similarvar ReactDI = ; // If you're just loading the file without any dependency management libraryvar ReactDI = windowReactDI; // Create an instance of resolvervar resolver = ; // Passing an object hash will automatically register// all of its propertiesvar resolver = message: aMessageService alert: anAlertService;
ReactJS setup (preferred)
Despite its name, you can use ReactDI without ReactJS. However, if you do, ReactDI provides a way to make your dependency injection much more straightforward.
After you have obtained an instance of ReactDI, you have to explicitly tell it to work with React object.
resolver;
This causes ReactDI to intercept every call to React.createElement
. ReactDI then enhances your
element's props
with di
property. This property is a callable function that proxies calls to resolver.get()
method (See below).
As a consequence, you can access your dependencies in your component methods:
React;
Furthermore, calling di()
without any arguments returns a reference to your ReactDI instance, so you can do some advanced logic too:
React;
Dependency validation
Only available when using development version of ReactDI.
If you create your React component class with statics.dependencies
property, ReactDI checks
whether all required dependencies are available on resolver. If not, warning is displayed.
Furthermore, dependencies declared this way are available as read-only properties on di
object:
React;
Dependency aliasing
If you create your React component class with statics.dependencies
set to an object hash,
your dependencies will be available on di
object under their aliases based on this object:
React;
Removing ReactDI injection
To remove React.createElement
interception (you should rarely need to do this), just call
resolver;
ReactJS independent setup
You can use ReactDI without automatic injection. You can simply take your ReactDI instance
and pass it around via props
(in ReactJS) or by any other means in any other framework
or just plain old vanilla JavaScript.
This might allow you to pass different resolvers to different components for example.
To use it with React, try this:
var Application = React; // Pass the resolver to componentsReact;
API
constructor
new ReactDI(
[object|ReactDI map]
)
map
is an existing ReactDI instance or an object hash of dependencies
that will be registered on this instance. Any properties registered on this passed instance (or added to this passed object)
will be automatically available on created instance.
.set(name, component)
reactDI set(
String|Object name,
[Object component]
)
When passed a String
and an object, this method registers component
under the name name
. If component
is null, it will be unregistered.
Wen passed an Object
as the first parameter its keys will be used as names and corresponding values as components.
This method returns the ReactDI
instancefor easy chaining.
.has(name)
Boolean has(
String name
)
Returns true
if a component with given name was registered on this resolver, false
if not.
.hasAny(names)
String hasAny(
String|Array[String] names
)
Returns the name of first resolved component if at least one of components with given name was registered on this resolver, false
if not.
.get(names)
Object get(
String|Array[String] names
)
When called with String
this method returns a component registered under this name
.
When called with an array of names this method returns an Object
with its keys corresponding to array elements.
This method throws an error if any of requested components was not registered.
.getAny(names)
Object getAny(
String|Array[String] names
)
Returns first resolved component or throws an error if none of requested components was registered.