React@16.x(57)Redux@4.x(6)- 实现 bindActionCreators

1,分析

一般情况下,action 并不是一个写死的对象,而是通过函数来获取。

bindActionCreators 的作用:为了更方便的使用创建 action 的函数 actionCreator

举例:

1,直接传入函数

const createAddUserAction = (user) => ({
    type: ADD_USER,
    payload: user,
});

store.dispatch(createAddUserAction({ id: 3, name: "name3", age: 20 }));

相当于

const addUser = bindActionCreators(createAddUserAction, store.dispatch);
addUser({ id: 3, name: "name3", age: 20 });

2,传入对象

const actions = {
    addUser: createAddUserAction,
    updateUser: createUpdateUserAction,
    deleteUser: createDeleteUserAction,
};

store.dispatch(actions.addUser({ id: 3, name: "name3", age: 20 }));
store.dispatch(actions.updateUser(2, { name: "name22" }));
store.dispatch(actions.deleteUser(1));

相当于

const bindAction = bindActionCreators(actions, store.dispatch);
bindAction.addUser({ id: 3, name: "name3", age: 20 });
bindAction.updateUser(2, { name: "name22" });
bindAction.deleteUser(1);

2,实现

export const bindActionCreators = (actionCreators, dispatch) => {
    if (typeof actionCreators === "function") {
        return getAutoDispatchActionCreator(actionCreators, dispatch);
    } else if (typeof actionCreators === "object") {
        const res = {};
        for (const key in actionCreators) {
            if (Object.hasOwnProperty.call(actionCreators, key)) {
                const ac = actionCreators[key];
                if (typeof ac === "function") {
                    res[key] = getAutoDispatchActionCreator(ac, dispatch);
                }
            }
        }
        return res;
    } else {
        throw TypeError("actionCreators 必须是函数或对象");
    }
};

function getAutoDispatchActionCreator(actionCreator, dispatch) {
    return function (...args) {
        dispatch(actionCreator(...args));
    };
}

以上。

相关推荐

  1. React@16.x57Redux@4.x6)- 实现 bindActionCreators

    2024-07-15 06:28:02       25 阅读
  2. React@16.x54Redux@4.x(3)- reducer

    2024-07-15 06:28:02       22 阅读
  3. React@16.x56Redux@4.x(5)- 实现 createStore

    2024-07-15 06:28:02       16 阅读
  4. React@16.x58Redux@4.x(7)- 实现 combineReducers

    2024-07-15 06:28:02       19 阅读
  5. React@16.x55Redux@4.x4)- store

    2024-07-15 06:28:02       21 阅读
  6. React@16.x53Redux@4.x(2)- action

    2024-07-15 06:28:02       21 阅读
  7. React@16.x52Redux@4.x(1)- 核心概念

    2024-07-15 06:28:02       19 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 06:28:02       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 06:28:02       56 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 06:28:02       46 阅读
  4. Python语言-面向对象

    2024-07-15 06:28:02       57 阅读

热门阅读

  1. PyTorch构建一个肺部CT图像分类模型来分辨肺癌

    2024-07-15 06:28:02       16 阅读
  2. Python学生信息管理系统的设计与实现

    2024-07-15 06:28:02       26 阅读
  3. SQL优化

    SQL优化

    2024-07-15 06:28:02      29 阅读
  4. RocketMQ

    RocketMQ

    2024-07-15 06:28:02      22 阅读
  5. SpringBoot实战:定时任务

    2024-07-15 06:28:02       19 阅读
  6. .NET 开源库技术栈汇总

    2024-07-15 06:28:02       16 阅读
  7. UDP 报文结构与注意事项全解析

    2024-07-15 06:28:02       24 阅读
  8. 深入理解Symfony框架中的数据验证机制

    2024-07-15 06:28:02       19 阅读
  9. OpenCV——实现视频图像的来回摆动的效果

    2024-07-15 06:28:02       14 阅读
  10. 【c++】VSstudio win32 应用开发

    2024-07-15 06:28:02       20 阅读