mobx.space

Derived state should be boring

2 November 2026

Most state bugs I have fixed this year were the same bug: a value that could have been computed was stored instead, and then drifted from its source.

The rule I keep coming back to is simple. If a value can be expressed as a function of other state, it is not state. Write the function, let the library cache it, and delete the setter.

get total() {\n  return this.items.reduce((s, i) => s + i.price * i.qty, 0)\n}

It reads like nothing, which is the point. The interesting code should be in the places where something actually changes.