all the application state is stored as a single object。
Redux的三大定律:
1.单一数据源。
2.state是只读的,改变state的唯一方法就是触发action,action其实就是一个普通的javascript对象。
3.使用纯函数执行修改,这里所说的纯函数就是Redux中reducer。
Three Principles
1、Single source of truth
The state of your whole application is stored in an object tree within a single store.
2、State is read-only
The only way to change the state is to emit an action, an object describing what happened.
3、Changes are made with pure functions
To specify how the state tree is transformed by actions, you write pure reducers.
- Actions
Actions are payloads of information that send data from your application to your store. They are the only source of information for the store. You send them to the store using store.dispatch().
Actions are plain JavaScript objects. Actions must have a type property that indicates the type of action being performed.
{
type: ADD_TODO,
payload: 'Build my first Redux app'
}
- Reducers
Reducers specify how the application's state changes in response to actions sent to the store. Remember that actions only describe what happened, but don't describe how the application's state changes.
- We don't mutate the state. We create a copy with Object.assign(). Object.assign(state, { visibilityFilter: action.filter }) is also wrong: it will mutate the first argument. You must supply an empty object as the first parameter. You can also enable the object spread operator proposal to write { ...state, ...newState } instead.
- We return the previous state in the default case. It's important to return the previous state for any unknown action.
- Store
The Store is the object that brings them together. The store has the following responsibilities:
- Holds application state;
- Allows access to state via getState();
- Allows state to be updated via dispatch(action);
- Registers listeners via subscribe(listener);
- Handles unregistering of listeners via the function returned by subscribe(listener).
It's important to note that you'll only have a single store in a Redux application. When you want to split your data handling logic, you'll use reducer composition instead of many stores.
IMG_1476.GIF
网友评论