Derivations

Anything that can be derived from the state without any further interaction is a derivation. Derivations exist in many forms:

  • The user interface.
  • Derived data, such as the number of todos left.

  • Backend integrations like sending changes to the server.

MobX distinguishes two kinds of derivations:

  • Computed values. These are values that can always be derived from the current observable state using a pure function.
  • Reactions. Reactions are side effects that need to happen automatically if the state changes. These are needed as a bridge between imperative and reactive programming. Or to make it more clear, they are ultimately needed to achieve I/O.

People starting with MobX tend to use reactions too often. The golden rule is: if you want to create a value based on the current state, use computed.

Back to the spreadsheet analogy, formulas are derivations that compute a value. But for you as a user to be able to see it on the screen, a reaction is needed that repaints part of the GUI.

results matching ""

    No results matching ""