Working with MobX#
Introduction#
Lens uses MobX on top of React's state management system.
The result is a more declarative state management style, rather than React's native setState
mechanism.
You can review how React handles state management here.
The following is a quick overview:
React.Component
is generic with respect to bothprops
andstate
(which default to the empty object type).props
should be considered read-only from the point of view of the component, and it is the mechanism for passing in arguments to a component.state
is a component's internal state, and can be read by accessing the super-class fieldstate
.state
must be updated using thesetState
parent method which merges the new data with the old state.- React does some optimizations around re-rendering components after quick successions of
setState
calls.
How MobX Works:#
MobX is a package that provides an abstraction over React's state management system. The three main concepts are:
observable
is a marker for data stored in the component'sstate
.action
is a function that modifies anyobservable
data.computed
is a marker for data that is derived fromobservable
data, but that is not actually stored. Think of this as computingisEmpty
rather than an observable field calledcount
.
Further reading is available on the MobX website.