目录

第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>
  );
}
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
胡八一8 分钟前
Window调试 ios 的 Safari 浏览器
前端·ios·safari
Dontla8 分钟前
前端页面鼠标移动监控(鼠标运动、鼠标监控)鼠标节流处理、throttle、限制触发频率(setTimeout、clearInterval)
前端·javascript
再学一点就睡19 分钟前
深拷贝与浅拷贝:代码世界里的永恒与瞬间
前端·javascript
CrimsonHu36 分钟前
B站首页的 Banner 这么好看,我用原生 JS + 三大框架统统给你复刻一遍!
前端·javascript·css
Enti7c39 分钟前
前端表单输入框验证
前端·javascript·jquery
拉不动的猪1 小时前
几种比较实用的指令举例
前端·javascript·面试
麻芝汤圆1 小时前
MapReduce 的广泛应用:从数据处理到智能决策
java·开发语言·前端·hadoop·后端·servlet·mapreduce
与妖为邻1 小时前
自动获取屏幕尺寸信息的html文件
前端·javascript·html
sunn。2 小时前
自定义组件触发饿了么表单校验
javascript·vue.js·elementui
烛阴2 小时前
Express入门必学三件套:路由、中间件、模板引擎全解析
javascript·后端·express