《React后台管理系统实战:九》Redux原理:异步实现【redux-thunk】、redux工具、合并多个reducer函数combineReducers()(三)

本文主要是介绍《React后台管理系统实战:九》Redux原理:异步实现【redux-thunk】、redux工具、合并多个reducer函数combineReducers()(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、redux-thunk基础

作用:在 redux 中执行异步任务(ajax, 定时器)

1)安装

cnpm install --save redux-thunk

2)使用:在redux/store.js中

//redux最核心的管理对象: store
import {createStore, applyMiddleware} from 'redux' //【0】引入applyMiddleware
import thunk from 'redux-thunk' // 【1】用来实现redux异步的redux中间件插件
import reducer from './reducer'export default createStore(reducer, applyMiddleware(thunk)) // 【2】创建store对象内部会第一次调用reducer()得到初始状态值

3 ) 在redux/action.js中添加如下

/*
包含n个用来创建action的工厂函数(action creator)*/
import {INCREMENT, DECREMENT} from './action-types'/*
增加的同步action*/
export const increment = number => ({type: INCREMENT, data: number})
/*
减少的同步action: 返回对象*/
export const decrement = number => ({type: DECREMENT, data: number})//【1】增加的异步action: 返回的是函数
export const incrementAsync = number => {return dispatch => {// 1. 执行异步(定时器, ajax请求, promise)setTimeout(() => {// 2. 当前异步任务执行完成时, 分发一个同步actiondispatch(increment(number))}, 1000)}
}

4)components/Counter.jsx

import React, {Component} from 'react'
import PropTypes from 'prop-types'/*
UI组件主要做显示与与用户交互代码中没有任何redux相关的代码*/
export default class Counter extends Component {static propTypes = {count: PropTypes.number.isRequired,increment: PropTypes.func.isRequired,decrement: PropTypes.func.isRequired,incrementAsync: PropTypes.func.isRequired, //【1】接收}constructor(props) {super(props)this.numberRef = React.createRef()}increment = () => {const number = this.numberRef.current.value * 1this.props.increment(number)}decrement = () => {const number = this.numberRef.current.value * 1this.props.decrement(number)}incrementIfOdd = () => {const number = this.numberRef.current.value * 1if (this.props.count % 2 === 1) {this.props.increment(number)}}//【2】异步函数incrementAsync = () => {const number = this.numberRef.current.value * 1this.props.incrementAsync(number)}render() {const count = this.props.countreturn (<div><p>click {count} times</p><div><select ref={this.numberRef}><option value="1">1</option><option value="2">2</option><option value="3">3</option></select> &nbsp;&nbsp;<button onClick={this.increment}>+</button>&nbsp;&nbsp;<button onClick={this.decrement}>-</button>&nbsp;&nbsp;<button onClick={this.incrementIfOdd}>increment if odd</button>&nbsp;&nbsp;<button onClick={this.incrementAsync}>increment async</button></div></div>)}
}

5)containers/App.jsx

import React, {Component} from 'react'
import {connect} from 'react-redux' //引入连接模块
import Counter from '../components/Counter' //引入components下的counter.jsx 注意路径
import {increment, decrement,incrementAsync} from '../redux/actions' //【1】引入redux下的动作incrementAsync// 指定向Counter传入哪些一般属性(属性值的来源就是store中的state)
const mapStateToProps = (state) => ({count: state})
// 指定向Counter传入哪些函数属性
/*如果是函数, 会自动调用得到对象, 将对象中的方法作为函数属性传入UI组件*/
/*const mapDispatchToProps = (dispatch) => ({increment: (number) => dispatch(increment(number)),decrement: (number) => dispatch(decrement(number)),
})*/
/*如果是对象, 将对象中的方法包装成一个新函数, 并传入UI组件 */
const mapDispatchToProps = {increment, decrement}/*
export default connect(mapStateToProps,mapDispatchToProps
)(Counter)*///【2】把incrementAsync添加进去
export default connect(state => ({count: state}),{increment, decrement,incrementAsync},
)(Counter)

效果:

在这里插入图片描述

二、redux开发工具安装

1)下载地址

https://dl.pconline.com.cn/download/2564119-1.html

2)直接拖进浏览器即可

谷歌最新不行就把插件名改为.zip格式解压出来,再到开发中心添加解压后的文件夹即可

3)f12即可看到redux,还不显示则进入4、5步;否则不用进行4、5步

4)安装

cnpm install --save-dev redux-devtools-extension

5)在redux/store.js添加如下

// redux最核心的管理对象: store,
import {createStore, applyMiddleware} from 'redux'
import reducer from './reducer' //导入reducer
import thunk from 'redux-thunk' // 用来实现redux异步的redux中间件插件
import {composeWithDevTools} from 'redux-devtools-extension' //【1】引入工具export default createStore(reducer,composeWithDevTools(applyMiddleware(thunk))) // 【2】再包一层composeWithDevTools();创建store对象内部会第一次调用reducer()得到初始状态值

效果:

在这里插入图片描述

三、combineReducers()合并多个reduce函数

1)在redux/reducer.js中

/*
reducer函数模块: 根据当前state和指定action返回一个新的state*/
import {combineReducers} from 'redux' //【1】引入
import {INCREMENT, DECREMENT} from './action-types'/*
管理count状态数据的reducer*/
function count(state=1, action) {console.log('count()', state, action)switch (action.type) {case INCREMENT:return state + action.datacase DECREMENT:return state - action.datadefault:return state}}const initUser = {}
/*
【2】再写一个状态。管理user状态数据的reducer*/
function user(state = initUser, action) {switch (action.type) {default:return state}
}/*
【3】合并多个状态;combineReducers函数: 接收包含所有reducer函数的对象, 返回一个新的reducer函数(总reducer)
总的reducer函数管理的state的结构{count: 2,user: {}}*/
export default combineReducers({count,user
})

2 ) containers/App.js写法要改成对应的对象

import React, {Component} from 'react'
import {connect} from 'react-redux'import Counter from '../components/Counter'
import {increment, decrement, incrementAsync} from '../redux/actions'// 指定向Counter传入哪些一般属性(属性值的来源就是store中的state)
const mapStateToProps = (state) => ({count: state.count}) //【1】此处对应读取对象的 state.count
// 指定向Counter传入哪些函数属性
/*如果是函数, 会自动调用得到对象, 将对象中的方法作为函数属性传入UI组件*/
/*const mapDispatchToProps = (dispatch) => ({increment: (number) => dispatch(increment(number)),decrement: (number) => dispatch(decrement(number)),
})*/
/*如果是对象, 将对象中的方法包装成一个新函数, 并传入UI组件 */
const mapDispatchToProps = {increment, decrement}/*
export default connect(mapStateToProps,mapDispatchToProps
)(Counter)*/export default connect(state => ({count: state.count}), //【2】此处对应读取对象的 state.count{increment, decrement, incrementAsync},
)(Counter)

这篇关于《React后台管理系统实战:九》Redux原理:异步实现【redux-thunk】、redux工具、合并多个reducer函数combineReducers()(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/965210

相关文章

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、