MobX
MobX allows mutating state, in opposition to Redux.
Packages
The mobx-react-lite
package adds observers, which is a capability of observing
store observables by the components. THose are necessary for apps to work
correctly. mobx-react-lite
supports only function components and
mobx-react
supports class components additionally.
Decorators
MobX allows to use decorators for indicating oservales, actions, etc. It’s an
experimental feature and it needs to be enabled in tsconfig.ts
, like this:
Store Example
In the component:
Modifying state
MobX recommends to mutate state only inside actions. If using ASYNC code in
actions state might be mutated outside of action! Solution is to use
runInAction
method after any async. Example:
In order to see errors when we mutate state outside of actions, we can do:
in a global context.