Principles
Redux rests on three principles:
- Single source of truth: We have one place where all our data lives.
- State is read-only: No mutation; there is only one way to change state and that is through an action.
- Changes are made with pure functions: A new state is produced by taking the old state, applying the change, and producing the new state; the old state is never changed.
Let's explore these bullet points one by one.
Single source of truth
The data lives in a single store in Redux and not a multiple store like in Flux. The data is represented by one object tree. This brings about a lot of benefits, such as:
- It is easier to see what your application knows at any given point, so it is easy to serialize or deserialize it.
- It is easier to work with in development, and easier to debug and inspect.
- It easier to do things such as undo/redo if all applied actions produce a new state.
An example of a single store can look like the following:
// principles/store.js class Store { getState() { ...