欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > react antd redux 全局状态管理 解决修改菜单状态 同步刷新左侧菜单

react antd redux 全局状态管理 解决修改菜单状态 同步刷新左侧菜单

2024/10/25 16:17:51 来源:https://blog.csdn.net/WithCYwind/article/details/142899052  浏览:    关键词:react antd redux 全局状态管理 解决修改菜单状态 同步刷新左侧菜单
npm i react-redux

1.src新建两个文件 globalState.js 全局状态定义 store.js 全局存储定义

在这里插入图片描述

2.globalState.js

import { createSlice } from "@reduxjs/toolkit";export const globalState = createSlice({name: "globalState",initialState: { data: {} },reducers: {//定义的函数 方便操作data对象update: (state, action) => {state.data = action.payload;},},
});
// 每个 case reducer 函数会生成对应的 Action creators
export const { update } = globalState.actions;export default globalState.reducer;

3.store.js

import { configureStore } from '@reduxjs/toolkit'
import globalState from './globalState'export default configureStore({reducer: {globalState: globalState}
})

4.读取状态

import { useSelector, useDispatch } from "react-redux";
const state = useSelector((state) => state.globalState);
console.log(state)

5.存储状态

import { useDispatch } from "react-redux";
import { update } from "../../../../state/globalState";//调度 update函数  传入想要存储的数据对象
dispatch(update({ reloadMenu: { id: r.id, reload: c } }));

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com