react18中引入redux及react-redux来管理公共数据仓库

前面已经介绍了redux的用法,但是在实际项目中,还是不够用的,不仅写法操作比较繁琐,而且实现的功能还有限,这篇我们引入官方推荐的react-redux来简化我们的代码

实现效果

代码改造

  • 安装相关依赖
bash 复制代码
npm i react-redux

store.js改写

js 复制代码
import { createStore } from "redux";
import { combineReducers } from "redux-immutable";

import { CounterReducer } from "./CounterReducer";

const reducers = combineReducers({
  count: CounterReducer,
});

const store = createStore(reducers);

export default store;

CounterReducer.js改写

js 复制代码
import { fromJS } from "immutable";
const initialState = fromJS({
  counter: 0,
  userInfo: {
    name: "John Doe",
    age: 25,
  },
});
function CounterReducer(state = initialState, action) {
  console.log("🚀 ~ CounterReducer ~ action:", action);
  switch (action.type) {
    case "ADD":
      return state.update("counter", (val) => {
        return val + 1;
      });
    case "DEC":
      return state.update("counter", (val) => val - 1);
    case "CHANGE_NAME":
      return state.setIn(["userInfo", "name"], action.payload);
    default:
      return state;
  }
}

export { CounterReducer };

DemoA.js

js 复制代码
import { useSelector } from "react-redux";

function DemoA() {
  const count = useSelector((state) => state.getIn(["count", "counter"]));
  return (
    <div>
      <p> Demo A count: {count}</p>
    </div>
  );
}
export default DemoA;

DemoB.js

js 复制代码
import { useSelector, useDispatch } from "react-redux";
import { Button, Space, Divider } from "antd";
function DemoB() {
  const count = useSelector((state) => state.getIn(["count", "counter"]));
  const userInfo = useSelector((state) => state.getIn(["count", "userInfo"]));
  const dispatch = useDispatch();
  return (
    <div>
      <p>Demo B</p>
      <div>
        <Space split={<Divider />}>
          <span>name: {userInfo.get("name")}</span>
          <span>age: {userInfo.get("age")}</span>
          <span>count:{count}</span>
        </Space>
      </div>
      <Space>
        <Button type="primary" onClick={() => dispatch({ type: "ADD" })}>
          add count
        </Button>
        <Button type="primary" danger onClick={() => dispatch({ type: "DEC" })}>
          minus count
        </Button>
        <Button
          type="dashed"
          danger
          onClick={() => dispatch({ type: "CHANGE_NAME", payload: "张学友" })}
        >
          change name
        </Button>
      </Space>
    </div>
  );
}
export default DemoB;

index.js

js 复制代码
import DemoA from "./DemoA";
import DemoB from "./DemoB";
import store from "../../store/index";
import { useEffect, useState } from "react";
import { Divider, Space } from "antd";
import { useSelector } from "react-redux";
function Redux() {
  const counter = useSelector((state) => state.getIn(["count", "counter"]));
  return (
    <div>
      Redux parent count:{counter}
      <Divider />
      <Space split={<Divider type="vertical" />}>
        <DemoA />
        <DemoB />
      </Space>
    </div>
  );
}

export default Redux;

引入了react-redux后的,代码的确是清爽了很多,没有之前的臃肿了。

总结

  • useSelector获取store中的数据
  • useDispatch触发reducer中的方法
  • immutablefromJS方法可以去官网参考它对不同类型的数据操作使用
相关推荐
小呆i10 分钟前
Vue生成名片二维码带logo并支持下载
前端·javascript·vue.js
tian-ming1 小时前
(五)Web前端开发进阶2——AJAX
前端
盒马盒马1 小时前
Ajax:跨域 & JSONP
前端·ajax
QEasyCloud20221 小时前
简道云和企业微信数据同步集成案例
java·大数据·前端
Cwhat1 小时前
通过自定义指令实现图片懒加载
前端·javascript·vue.js
WindrunnerMax1 小时前
Canvas简历编辑器-选中绘制与拖拽多选交互设计
前端·javascript·编辑器
小彭努力中2 小时前
102. 管道漫游案例
前端·3d·webgl
小彭努力中2 小时前
107. 阴影范围.shadow.camera
前端·深度学习·3d·webgl
INFINI Labs2 小时前
Tauri(一)——更适合 Web 开发人员的桌面应用开发解决方案 ✅
前端·electron·tauri·web开发人员·web桌面应用
十一吖i2 小时前
vue 实现图片预览功能并显示在弹窗的最上方
前端·javascript·vue.js