第47节——使用bindActionCreators封装actions模块

一、什么是action creators

1、概念

在Redux中,Action Creators是一种函数,它用于创建一个描述应用程序状态变化的action对象。Action对象是一个普通JavaScript对象,它包含一个描述action类型的字符串属性(通常称为"type"),以及与该操作相关的其他属性。

Action Creators 是 Redux 应用程序的重要部分,因为它们提供了一种清晰和标准化的方式来描述应用程序中的操作,并将这些操作转化为 Redux 可以理解的形式。此外,Action Creators 还允许应用程序的各个部分之间进行通信,包括 React 组件和 Redux Store。

2、例子

一下部分就是一个简单的action creators

jsx 复制代码
function incrementCounter() {
  return { 
    type: 'INCREMENT_COUNTER' 
  };
}

二、什么是bindActionCreators

1、概念

bindActionCreators 是 Redux 中的一个函数,用于将多个 action creators 绑定到 dispatch 函数上,使得可以在 Redux 应用中轻松调用这些 action creators。

2、基本语法

jsx 复制代码
/**
 * 接收两个参数
 * 第一个参数 actionCreators:一个 Action Creator 函数或包含多个 Action Creator 函数的对象。
 * 如果是对象,则对象的键名将用作 Action Creator 函数的名称。
 * dispatch:Redux Store 的 dispatch 函数。
 * 
 * bindActionCreators 的返回值是一个对象,
 * 这个对象包含了与原始 action creators 同名的函数,
 * 但这些函数在调用时会自动派发一个 action,而不需要手动调用 dispatch。
 * 这个返回的对象可以直接作为组件的 props 传递给 React 组件使用
 */
const actions = bindActionCreators({ incrementCounter }, dispatch);

三、封装一个userActions hooks

将多个 Action Creator 函数绑定到 Redux store 的 dispatch 函数上,使它们能够被在组件中直接调用。

jsx 复制代码
import { useMemo } from "react";
import { useDispatch } from "react-redux";
import { bindActionCreators } from "redux";

/**
 *
 * @param {} actionCreators
 * 接收一个actionCreators对象
 * @returns
 */
export const useAction = (actionCreators) => {
  const dispatch = useDispatch();
  return bindActionCreators(actionCreators, dispatch);
};

// 进阶使用useMemo
// export const useAction = (actionCreators) => {
//   const dispatch = useDispatch();
//   return useMemo(() => {
//     console.log("调用了");
//     return bindActionCreators(actionCreators, dispatch);
//   }, [actionCreators, dispatch]);
// };

四、基本使用

1、创建actions目录,并在目录里创建user.js模块

jsx 复制代码
import { bindActionCreators } from "redux";
import { useDispatch } from "react-redux";

function addAge() {
  return {
    type: "user/addAge",
  };
}

export default {
  addAge,
};

2、在reducer模块中添加user.js模块

jsx 复制代码
import produce from "immer";

const defaultState = {
  name: "李光明",
  age: 20,
};

export const userReducer = (state = defaultState, action) => {
  switch (action.type) {
    /**
     * reducer模块化后命名
     * 一般要求全局唯一
     * 一般来可以采用模块名/case名的方式
     */
    case "user/addAge":
      // 如果使用了之间封装的immer中间件则必须使用immer更新
      // return produce(state, draft => {
      //   draft.age += 1
      // })
      return {...state, age: state.age + 1}
    default:
      return state;
  }
};

3、页面中使用

jsx 复制代码
import { useSelector } from "react-redux";
import { useAction } from "./store/utils";
import userActionCreators from "./store/actions/user";
import { compose } from "redux";

export default function LearnRedux3() {
  const state = useSelector((state) => state.user);

  /**
   * 所以的action最终都要使用dispath分发
   * 那么我就使用直接把action creator 和 dispath绑定
   * 在页面中直接使用
   */
  const action = useAction(userActionCreators);
  const add = () => {
    console.log(action);
    action.addAge();
  };

  return (
    <div>
      <div>年龄:{state.age}</div>
      <button onClick={() => add()}>过年</button>
    </div>
  );
}
相关推荐
子兮曰4 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰4 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万4 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝4 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋4 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁4 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄4 天前
JavaScript 隐式全局变量解析
javascript
汉堡大王95274 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大4 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师4 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端