Redux: Principles

 20th August 2020 at 2:19pm

使用 Redux 需要遵循的原则。

Single source of truth

The state of your whole application is stored in an object tree within a single store.

好处是:

  • Server 端的数据可以很简单地合入 client 端
  • 调试更容易了
  • 开发阶段可以做一些数据保持,加快开发周期
  • Undo / Redo 等本来比较难做的功能,也容易做了

State is read-only

The only way to change the state is to emit an action, an object describing what happened.

好处是所有操作都有顺序,而且可被纪录、序列化、存储及后续追溯。

Changes are made with pure functions

To specify how the state tree is transformed by actions, you write pure reducers.

好处显而易见,不加入副作用,才能保证整个 action 处理过程是稳定的。

Resources