React中redux、react-redux、@reduxjs/toolkit状态管理库的使用方式

embedded/2024/9/23 17:28:48/

效果

在这里插入图片描述

下载依赖

javascript">npm install redux react-redux @reduxjs/toolkit --save

在src目录下创建文件

在这里插入图片描述

  1. 创建index.ts文件
import { configureStore } from '@reduxjs/toolkit'
import userSlice from './userReducer'const store = configureStore({reducer: {user: userSlice.reducer}
})
// 订阅 store
// store.subscribe(() => console.log('subscribe: ', store.getState()))export default store
  1. 创建userReducer.ts文件
javascript">import { createSlice } from '@reduxjs/toolkit'const userSlice = createSlice({name: 'user',initialState: {str: '我是redux未修改前的文字'},reducers: {editStr(state, action) {// Redux Toolkit 允许在 reducers 中编写 "mutating" 逻辑。// 它实际上并没有改变 state,因为使用的是 Immer 库,检测到“草稿 state”的变化并产生一个全新的,// 基于这些更改的不可变的 statestate.str = action.payload}}
})
export default userSlice

在入口文件中引用

import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import store from './store/index'const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement
);root.render(<Provider store={store}><App /></Provider>
);

在函数式组件中使用

import { useSelector, useDispatch } from 'react-redux'
import { useNavigate } from 'react-router-dom';
import React, { useState } from 'react';
import { Button } from 'antd';const Home: React.FC = () => {const navigate = useNavigate();const { str } = useSelector((state:StoreType.State) => state.user)let dispatch = useDispatch()const [msg] = useState<string>('点击改变redux');const handleChange = () => {dispatch({type: 'user/editStr',payload: '我是工作台修改redux后的值'})}return (<><h1>工作台</h1><Button type="primary" onClick={handleChange}>{msg}</Button><Button type="primary" onClick={() => navigate('/authMag/userMag')}>跳转到用户页面</Button><h1>{str}</h1></>) 
}export default Home

在类组件中使用

import React from "react"
import { Button } from 'antd';
import { connect } from "react-redux";
import { Link } from 'react-router-dom'type PropType = {user: {str: string},dispatch: Function
}type StateType = {msg: string
}class User extends React.Component<PropType, StateType> {constructor(props: PropType | Readonly<PropType>) {super(props)this.state = {msg: '点击改变redux'}}componentDidMount() {console.log('User-componentDidMount')}handleChange = () => {this.props.dispatch({type: 'user/editStr',payload: '我是User页修改redux后的值'})}render() {const { msg } = this.stateconst { str } = this.props.userreturn (<><h1>用户管理</h1><Button type="primary" onClick={this.handleChange}>{msg}</Button><Button type="primary"><Link to="/home">跳转到工作台页面</Link>        </Button><h1>{str}</h1></>)}
}
const mapStateToProps = (state:PropType) => ({user: state.user
});const mapDispatchToProps = (dispatch: Function) => ({dispatch
});
export default connect(mapStateToProps, mapDispatchToProps)(User);

http://www.ppmy.cn/embedded/7059.html

相关文章

Python 数据结构和算法实用指南(二)

原文&#xff1a;zh.annas-archive.org/md5/66ae3d5970b9b38c5ad770b42fec806d 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 第四章&#xff1a;列表和指针结构 我们已经在 Python 中讨论了列表&#xff0c;它们方便而强大。通常情况下&#xff0c;我们使用 Python…

【linux运维】系统常见管理命令

系列综述&#xff1a; &#x1f49e;目的&#xff1a;本系列是个人整理为了学习基本的shell编程和linux命令&#xff0c;整理期间苛求每个知识点&#xff0c;平衡理解简易度与深入程度。 &#x1f970;来源&#xff1a;材料主要源于b站大学——linux运维课程进行的&#xff0c;…

目标检测——食品饮料数据集

一、重要性及意义 对食品和饮料进行目标检测的重要性和意义体现在多个方面&#xff1a; 商业应用与市场分析&#xff1a;目标检测技术在食品和饮料行业有着广泛的应用前景。通过对超市货架、餐馆菜单或广告海报中的食品和饮料进行自动识别和计数&#xff0c;商家可以获取关于产…

OpenHarmony网络组件-Mars

项目简介 Mars 是一个跨平台的网络组件&#xff0c;包括主要用于网络请求中的长连接&#xff0c;短连接&#xff0c;是基于 socket 层的解决方案&#xff0c;在网络调优方面有更好的可控性&#xff0c;暂不支持HTTP协议。 Mars 极大的方便了开发者的开发效率。 效果演示 编译…

【IC前端虚拟项目】测试点思路和文档编写

【IC前端虚拟项目】数据搬运指令处理模块前端实现虚拟项目说明-CSDN博客 好&#xff0c;又到了测试点文档了这一部分了&#xff0c;我经常跟带着学习的童鞋说&#xff0c;对于芯片验证人来说测试点文档就是最重要的一个交付文件&#xff0c;也是在项目交付中最为看重的进度衡量…

海外媒体如何发布软文通稿

大舍传媒-带您了解海外发布新潮流 随着全球化的不断深入&#xff0c;越来越多的中国企业开始关注海外市场。为了在国际舞台上树立品牌形象&#xff0c;企业纷纷寻求与海外媒体合作&#xff0c;通过发布软文通稿的方式&#xff0c;传递正面信息&#xff0c;提升品牌知名度。作为…

书生·浦语大模型全链路开源体系-第5课

书生浦语大模型全链路开源体系-第5课 书生浦语大模型全链路开源体系-第5课相关资源LMDeploy基础配置LMDeploy运行环境下载internlm2-chat-1_8b模型使用Transformer来直接运行InternLM2-Chat-1.8B模型使用LMDeploy以命令行方式与InternLM2-Chat-1.8B模型对话设置KV Cache最大占用…

Compose 布局

文章目录 Compose 布局ColumnColumn属性使用 RowRow属性使用 BoxBox属性使用 ConstraintLayoutLazyColumnLazyColumn属性使用使用多类型使用粘性标题回到顶部 LazyRowLazyRow属性使用 LazyVerticalGridLazyVerticalGrid属性使用 Compose 布局 Column Compose中的”垂直线性布…