mobx-controller
React HOC (Higher-Order Component) based on Mobx-React Injector to Inject a controller.
A controller instance will be created each time the component will be mounted and will be destroyed when the component will unmount
Why do I need a controller?
- Separation of concerns
- When the State of the component in the controller it's easier to write tests ( no more async setState)
Installation
npm install mobx-controller --save
;
API documentation
controller(controllerName:string, controller: Class)
controller(controllerName:string, controller: Class, mapper: Function)
Function/Decorator that injects the controller instance as a prop to your React component.
Example: Injects controller as prop
;; // ---- ES6 syntax ---- { // the constructor get all the stores thisallStores = allStores; } user: name: 'Jack' console; const TodoView = // ---- ESNext syntax with decorators ----@@observerComponent { return <div>thispropsmyCtrlusername</div> }
Example: Using a custom mapper
{ return user: controlleruser } // ---- ES6 syntax ---- { // the constructor get all the stores thisallStores = allStores; } user: name: 'Jack' console; const TodoView = // ---- ESNext syntax with decorators ----@@observerComponent { return <div>thispropsCtrlusername</div> }
you can look at the demo for a full working example