理解 Redux 的单向数据流:Action → Reducer → Store → View
{
type: "{{ currentAction.type }}",
payload: {{ currentAction.payload || 'undefined' }}
}
function reducer(state, action) {
switch (action.type) {
case "{{ currentAction.type }}":
return {
...state,
count: (state?.count ?? 0) {{ currentAction.operator }} {{ currentAction.step || 1 }}
};
default:
return state;
}
}
{
count: {{ count }}
}
整个应用的 state 储存在唯一的 store 中
唯一改变 state 的方法是触发 action
Reducer 必须是纯函数,接收旧 state 返回新 state