更新Redux中状态的流程是这样的:action -> reducer -> new state。
每次action被触发(dispatch),reducer就会同步地对store进行更新
action: 指全局发布的动作指令,主要就是定义所有事件行为的,例如:
export const ADD_NOTE = "ADD_NOTE";
export const DELETE_NOTE = "DELETE_NOTE";
let nextTodoId=0;
export const addNote = (title,text) => ({
type: ADD_NOTE,
id: nextTodoId++,
title:title,
note: text
});
export const deleteNote = (id) => ({
type: DELETE_NOTE,
id: id
});
以上定义了两个action函数,分别是添加笔记和删除笔记,正如前面所说,mapDispatchToProps 将 action 作为 props 绑定到组件,可以直接在组件中调用触发dispatch,代码如下: