使用actionCreator统一创建action

1.在store文件夹下创建actionCreator,js文件
代码如下
import { CHANGE_INPUT_VALUE , ADD_TODO_ITEM , DELET_TODO_ITEM } from './ActionTypes';
export const getInputChangeAction = (value) => ({
    type: CHANGE_INPUT_VALUE,
    value
})
export const getAddItemAction = () => ({
    type : ADD_TODO_ITEM
})
export const getDeletItemAction = (index) => ({
    type : DELET_TODO_ITEM,
    index
})

2.TodoList文件中引入actionCreator
代码如下
import { getInputChangeAction , getAddItemAction , getDeletItemAction} from './store/actionCreators.js';

3.更改TodoList文件中的action
    handleChange (e) {
        const action = getInputChangeAction(e.target.value)
        store.dispatch(action);
    }
    handleAddList() {
        const action = getAddItemAction();
        store.dispatch(action)
    }
    handleDelet (index) {
        const action = getDeletItemAction(index);
        store.dispatch(action)
    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容