Maintaining state is one of the big challenges faced by any app. as the app gets bigger, the tasks becomes more complex
Before we dive in
Decorators (higher order functions)
Class properties
Class fat arrow methods
refresher:
ES6 getters/setters
@observer
class Counter {
count = 0;
constructor() {
super()
}
handleDec = () => {
this.count--;
}
handleInc = () => {
this.count++;
}
}
yarn add mobx mobx-react
// installing ES.next webpack plugins
yarn add babel-plugin-transform-class-properties --dev
yarn add babel-plugin-transform-decorators-legacy --dev
{
"presets":["react","es2015"],
"plugins":["transform-decorators-legacy", "transform-class-properties"]
}
.babelrc
Create a ticker using mobx
State
Derivations
Actions
Reactions
We can use React context mechanism to pass stores to components without passing the store through multiple layers of components: