【React】react-redux+redux-toolkit实现状态管理

news/2025/2/19 12:47:45/

安装

npm install @reduxjs/toolkit react-redux
  1. Redux Toolkit 是官方推荐编写Redux的逻辑方式,用于简化书写方式
  2. React-redux 用来链接Redux和React组件之间的中间件

使用

定义数据

创建要管理的数据模块 store/module/counter.ts

import { createSlice, PayloadAction } from '@reduxjs/toolkit'const counterSlice = createSlice({name: 'counter',initialState: {counter: 0, // 初始值},reducers: {// 修改值的方法changeCounter: (state, { payload }: PayloadAction<number>) => {state.counter = payload // 使传入的参数赋值到counter},}
})export const { changeCounter } = counterSlice.actions // 导出修改的方法
export default counterSlice.reducer

创建store/index.ts用于管理和导出项目所含的状态数据

import { configureStore } from '@reduxjs/toolkit'
import { useSelector, useDispatch } from 'react-redux'
import type { TypedUseSelectorHook } from 'react-redux'
import counterReducer from './module/counter' // 管理的模块const store = configureStore({reducer:{counter: counterReducer // 定义管理的模块}
})type RootState = ReturnType<typeof store.getState>
type AppDispatch = typeof store.dispatch
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector // 导出使用数据的方法
export const useAppDispatch: () => AppDispatch = useDispatch // 导出修改数据的方法
export default store

为React注入store

在项目主入口main.tsx中, 使用Provider标签包裹来注入store

import { Provider } from "react-redux";
import store from "@/store/index.ts";createRoot(document.getElementById("root") as HTMLElement).render(<Provider store={store}><App /></Provider>
);

页面中使用/修改数据

import { useAppSelector, useAppDispatch } from "@/store";
import { changeCounter } from "@/store/module/counter";const { count, text } = useAppSelector((state) => ({count: state.counter.counter, // 取值
}));// 修改方法
const dispatch = useAppDispatch();
function handlecChangeCount() {dispatch(changeCounter(count + 1));
}return (<><div>{count}</div><button onClick={handlecChangeCount}>change</button></>);

http://www.ppmy.cn/news/1572207.html

相关文章

基于SSM+uniapp的数学辅导小程序+LW示例参考

1.项目介绍 系统角色&#xff1a;管理员、普通用户功能模块&#xff1a;用户管理、学习中心、知识分类管理、学习周报管理、口算练习管理、试题管理、考试管理、错题本等技术选型&#xff1a;SSM&#xff0c;Vue&#xff08;后端管理web&#xff09;&#xff0c;uniapp等测试环…

海思3559a_使用2.0.4.0版SDK编译固件下载后i2c_write和i2c_read不支持怎么办

问题如下&#xff1a; 烧录完固件后想读取i2c寄存器内容发现不支持&#xff0c;如下&#xff1a; 解决方法如下&#xff1a; 进入Hi3559AV100_SDK_V2.0.4.0/osdrv/tools/board/reg-tools-1.0.0 在该目录下make 该目录下的bin文件内容如下&#xff1a; 将bin目录下的内容…

使用pocketpal-ai在手机上搭建本地AI聊天环境

1、下载安装pocketpal-ai 安装github的release APK 2、安装大模型 搜索并下载模型&#xff0c;没找到deepseek官方的&#xff0c;因为海外的开发者上传了一堆乱七八糟的deepseek qwen模型&#xff0c;导致根本找不到官方上传的……deepseek一开源他们觉得自己又行了。 点击之…

Microsoft Edge 浏览器调优

文章目录 一、基础环境二、适用场景三、过程方法 一、基础环境 操作系统&#xff1a;Microsoft Windows 10 / 11 和 Microsoft Windows Server 2019 / 2022 / 2025浏览器&#xff1a;Microsoft Edge 二、适用场景 安装 MicEdge之后&#xff0c;不希望 Edge 频繁更新以及退出…

【教程】比亚迪车机接入AI大模型语音助手

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 为什么需要​​​AI大模型语音助手&#xff1f;内置的小迪不行吗&#xff1f; 其实还是有用的&#xff0c;比如路上遇到鱼骨线等不知道什么意思&#x…

3D打印技术:如何让古老文物重获新生?

如何让古老文物在现代社会中焕发新生是一个重要议题。传统文物保护方法虽然在一定程度上能够延缓文物的损坏&#xff0c;但在文物修复、展示和传播方面仍存在诸多局限。科技发展进步&#xff0c;3D打印技术为古老文物的保护和传承提供了全新的解决方案。我们来探讨3D打印技术如…

Python爬虫实战:股票分时数据抓取与存储 (1)

在金融数据分析中&#xff0c;股票分时数据是投资者和分析师的重要资源。它能够帮助我们了解股票在交易日内的价格波动情况&#xff0c;从而为交易决策提供依据。然而&#xff0c;获取这些数据往往需要借助专业的金融数据平台&#xff0c;其成本较高。幸运的是&#xff0c;通过…

Vue3 从入门到精通:全面掌握前端框架的进阶之路

一、Vue3 简介 Vue.js 是一款流行的 JavaScript 前端框架&#xff0c;用于构建用户界面。Vue3 作为 Vue.js 的重大升级版本&#xff0c;带来了诸多性能提升和新特性。它采用了 Proxy 实现数据响应式系统&#xff0c;优化了虚拟 DOM 算法&#xff0c;使得应用在运行时更加高效。…