React状态管理——react-redux

目录

一、redux介绍

二、安装

三、基本实现步骤

3.1 创建Action Types

3.2 创建counterAction

3.3 创建counterReducer

3.4 结合所有Reducer

3.5 创建store

3.6 入口文件中提供store

3.7 在组件中的使用

3.8 使用thunk实现异步支持

3.8.1 安装

3.8.2 在counterAction中添加异步操作

3.8.3 在store文件中添加异步支持

3.8.4 在组件中进行异步操作

3.9 函数式组件使用hooks代替connect


一、redux介绍

Redux 基于三个基本原则:

  • 单一数据源:整个应用的状态存储在一个对象树中

  • 状态是只读的:唯一改变状态的方法是触发 action

  • 使用纯函数执行修改:reducer 是纯函数,接收旧 state 和 action,返回新 state

二、安装

npm install redux react-redux

三、基本实现步骤

3.1 创建Action Types

// actionTypes.js
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";

3.2 创建counterAction

// store/actions/counterAction.jsimport { INCREMENT, DECREMENT } from "../../utils/actionTypes";export const increment = () => ({type: INCREMENT,
});export const decrement = () => ({type: DECREMENT,
});

3.3 创建counterReducer

// store/reducers/counterReducer.jsimport { INCREMENT, DECREMENT } from "../../utils/actionTypes";const initialState = {count: 0,
};export default function counterReducer(state = initialState, action) {switch (action.type) {case INCREMENT:return { ...state, count: state.count + 1 };case DECREMENT:return { ...state, count: state.count - 1 };default:return state;}
}

3.4 结合所有Reducer

// store/reducers/index.jsimport { combineReducers } from "redux";
import counterReducer from "./counterReducer";export default combineReducers({counter: counterReducer,
});

3.5 创建store

// store/index.js
import { createStore } from "redux";
import reducers from "./reducers";const store = createStore(reducers);export default store;

3.6 入口文件中提供store

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

3.7 在组件中的使用

import React from "react";
import { connect } from "react-redux";
import { increment, decrement } from "./store/actions/counterAction";function App(props) {const { count, increment, decrement } = props;return (<div><div>数值:{count}</div><button onClick={() => increment()}>点我+1</button><button onClick={() => decrement()}>点我-1</button></div>);
}
const mapStateToProps = (state) => ({count: state.counter.count, // store/reducers/index.js中的counter映射
});const mapDispatchToProps = {increment,decrement,
};export default connect(mapStateToProps, mapDispatchToProps)(App);

3.8 使用thunk实现异步支持

3.8.1 安装

npm install redux-thunk

3.8.2 在counterAction中添加异步操作

// store/actions/counterAction.jsimport { INCREMENT, DECREMENT } from "../../utils/actionTypes";export const increment = () => ({type: INCREMENT,
});export const decrement = () => ({type: DECREMENT,
});export const fetchIncrement = (timeout) => {return (dispatch) => {setTimeout(() => {dispatch(increment());}, timeout);};
};

3.8.3 在store文件中添加异步支持

// store.js
import { createStore, applyMiddleware } from "redux";
import { thunk } from "redux-thunk";
import reducers from "./reducers";const store = createStore(reducers, applyMiddleware(thunk));export default store;

3.8.4 在组件中进行异步操作

import React from "react";
import { connect } from "react-redux";
import {increment,decrement,fetchIncrement,
} from "./store/actions/counterAction";function App(props) {const { count, increment, decrement, fetchIncrement } = props;return (<div><div>数值:{count}</div><button onClick={() => increment()}>点我+1</button><button onClick={() => decrement()}>点我-1</button><button onClick={() => fetchIncrement(1000)}>点我异步+1</button></div>);
}
const mapStateToProps = (state) => ({count: state.counter.count, // store/reducers/index.js中的counter映射
});const mapDispatchToProps = {increment,decrement,fetchIncrement,
};export default connect(mapStateToProps, mapDispatchToProps)(App);

3.9 函数式组件使用hooks代替connect

// App.jsimport React from "react";
import { useSelector, useDispatch } from "react-redux";
import {increment,decrement,fetchIncrement,
} from "./store/actions/counterAction";export default function App(props) {const { count } = useSelector((state) => state.counter); // store/reducers/index.js中的counter映射const dispatch = useDispatch();return (<div><div>数值:{count}</div><button onClick={() => dispatch(increment())}>点我+1</button><button onClick={() => dispatch(decrement())}>点我-1</button><button onClick={() => dispatch(fetchIncrement(1000))}>点我异步+1</button></div>);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/85071.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/85071.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 零工市场小程序 | 灵活就业平台 | 智能匹配 | 日结薪系统 | 用工一站式解决方案

在就业形势如此严峻的情况下&#xff0c;很多小伙伴都会选择零工的工作方式来赚取外快&#xff0c;很多用人单位也会因为职为短暂空缺或是暂时人手不够而选择招用兼职人员。 而Java 作为企业级开发的主流语言&#xff0c;以其卓越的性能和稳定性著称。把零工的需求&#xff08…

数据可视化——一图胜千言

第04篇&#xff1a;数据可视化——一图胜千言 写在前面&#xff1a;大家好&#xff0c;我是蓝皮怪&#xff01;前面几篇我们聊了统计学的基本概念、数据类型和描述性统计&#xff0c;这一篇我们要聊聊数据分析中最直观、最有趣的部分——数据可视化。你有没有发现&#xff0c;很…

1.1 Linux 编译FFmpeg 4.4.1

一、安装编译工具 sudo apt install -y autoconf automake build-essential cmake git pkg-config nasm yasm libtool zlib1g-dev说明&#xff1a; autoconf&#xff1a;生成 configure 脚本&#xff0c;用于自动配置源码。automake&#xff1a;与 autoconf 配合&#xff0c;…

【图片识别改名】如何批量识别大量图片的文字并重命名图片,基于WPF和京东OCR识别接口的实现方案

应用场景 在企业文档管理、数字图书馆、电商商品管理等场景中&#xff0c;经常需要处理大量图片中的文字信息。例如&#xff1a; 电商平台需要将商品图片中的型号、规格等信息提取出来作为文件名图书馆需要将扫描的图书页面识别为文字并整理归档企业需要将纸质文档电子化并按…

简历模板2——数据挖掘工程师5年经验

姓名 / Your Name 数据挖掘工程师 | 5年经验 | 推荐/风控/图模型 &#x1f4de; 138-XXXX-XXXX | ✉️ your.emailexample.com | &#x1f310; github.com/yourname | &#x1f4cd; 北京 &#x1f3af; 个人简介 / Summary 5年大厂数据挖掘经验&#xff0c;硕士学历。擅长推…

CSS3 渐变效果

1. 引言 CSS3 渐变能够在指定颜色之间创建平滑过渡效果。这种设计元素不仅能为网页增添丰富的视觉层次&#xff0c;更是现代网页设计的重要组成部分。CSS3 提供两种主要的渐变类型&#xff1a;线性渐变(Linear Gradient) - 沿直线方向进行颜色过渡&#xff1b;径向渐变(Radial…

A Survey on 3D Gaussian Splatting——3D高斯领域综述

原文链接&#xff1a;[2401.03890] A Survey on 3D Gaussian Splatting 动态更新的GitHub仓库&#xff08;包含性能对比与最新文献追踪&#xff09;&#xff1a; https://github.com/guikunchen/3DGS-Benchmarks https://github.com/guikunchen/Awesome3DGS 摘要&#xff1…

计算机网络 期末实训 eNSP 校园网

eNSP 综合实训 小型校园网 计算机网络期末实训 01 搭建拓扑 1.设计任务 构建一个小型校园网络,涵盖以下设备与区域: 学生宿舍区:50台计算机办公楼区:30台计算机(细分为财务部门、人事部门及其他科室)图书馆:10台计算机教学楼:30台计算机服务器集群:2台服务器,分别用…

Smart Form Adobe form 强制更改内表:TNAPR

强制更改内表:TNAPR se16-> Smart Form总览 Smart form 变量格式说明: &symbol& (括号中,小写字母为变量) &symbol& 屏蔽从第一位开始的N位 &symbol (n)& 只显示前N位 &symbol (S)& 忽略正负号 &symbol (<)& 符号在…

页面配置文件pages.json和小程序配置

页面配置文件pages.json和小程序配置 pages.jsonpagesstyle-navigationBarBackgroundColorstyle-navigationBarTitleTextstyle-navigationStylestyle-enablePullDownRefresh注意事项不同平台区分配置新建页面 globalStyletabBar代码 manifest.json授权web配置代理 pages.json …

Linux网络配置工具ifconfig与ip命令的全面对比

在Linux网络管理中&#xff0c;ifconfig和 ip命令是最常用的两个工具。随着时间的推移&#xff0c;ip命令逐渐取代了 ifconfig&#xff0c;成为更强大和灵活的网络配置工具。本文将对这两个工具进行全面对比&#xff0c;帮助您理解它们的区别和各自的优势。 一、ifconfig命令 …

STM32 实现解析自定义协议

一、环形队列设计与实现&#xff08;核心缓冲机制&#xff09; 数据结构设计&#xff1a; #define BUFFER_SIZE 512 #define BUFFER_MASK (BUFFER_SIZE - 1) typedef struct {volatile uint8_t buffer[BUFFER_SIZE]; // 环形缓冲区&#xff08;大小可配置&#xff09;volati…

NGINX 四层上游模块`ngx_stream_upstream_module` 实战指南

一、模块定位与引入 模块名称&#xff1a;ngx_stream_upstream_module 首次引入&#xff1a;NGINX 1.9.0&#xff08;2015-08-04&#xff09; 编译选项&#xff1a;启用 --with-stream&#xff08;含此模块&#xff09; 作用&#xff1a; 定义后端服务器组&#xff08;upstr…

WinUI3入门2:DataGrid动态更新 添加删除和修改字段

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 源码指引&#xff1a;github源…

基于Python学习《Head First设计模式》第十三章 现实世界中的模式

定义设计模式 设计模式要素 模式名称、分类意图&#xff1a;描述模式是什么动机&#xff1a;描述什么时候使用这个模式&#xff0c;具体场景适用性&#xff1a;描述什么地方使用这个模式&#xff0c;用在什么场合结构&#xff1a;类图参与者&#xff1a;类和对象的责任和角色…

线性代数(1)线性方程组的多种解法

求解线性方程组是线性代数的核心问题之一&#xff0c;根据方程组的类型&#xff08;如齐次/非齐次、方阵/非方阵、稀疏/稠密等&#xff09;&#xff0c;可以采用不同的解法。以下是常见的线性方程组解法分类及简要说明&#xff1a; 一、直接解法&#xff08;精确解&#xff09…

肝脏/肝脏肿瘤图像分割数据集(猫脸码客第261期)

探秘肝脏/肝脏肿瘤图像分割&#xff1a;医学影像技术的新突破 一、引言 肝脏/肝脏肿瘤图像分割在医学领域占据着愈发重要的地位&#xff0c;为肝脏疾病的精准诊断与有效治疗提供了关键技术支撑。随着医学成像技术的飞速进步&#xff0c;如磁共振成像&#xff08;MRI&#xff…

【LLM05---位置编码】

文章目录 位置编码引出Transformer中位置编码方法:Sinusoidal functions两个重要性质位置编码 最近在学习位置编码,想找一个讲的比较透彻的文章或视频,找了半天,满意的一个也没有,所以自己记录一下。 注意,本篇笔记只作为自己的学习记录用,更好的讲解的内容请看链接:位…

pikachu——ssrf

概念补充&#xff1a; 内网&#xff1a;局部范围内的私有网络&#xff0c;比如局域网就是一个小范围的内网&#xff0c;有私有IP&#xff0c;并且内网受防火墙的保护&#xff0c;外网无法直接访问 外网&#xff1a;全球范围的公共网络&#xff0c;公有ip ip地址&#xff1a;…

java 设计模式_行为型_13备忘录模式

13.备忘录模式 模式定义 备忘录模式&#xff08;Memento Pattern&#xff09;模式的定义&#xff1a;在不破坏封装性的前提下&#xff0c;捕获一个对象的内部状态&#xff0c;并在该对象之外保存这个状态&#xff0c;以便以后当需要时能将该对象恢复到原先保存的状态。该模式又…