2 中间件的使用、异步action的创建

react-redux是react插件
将所有组件分成两大类:UI组件和容器组件
安装npm install react-redux -S
UI组件:

  1. 负责UI的呈现,不带有任何业务逻辑
  2. 不使用this.state
  3. 所有数据都由this.props提供
  4. 不使用任何Redux的API,不需要使用store

容器组件:

  1. 负责管理数据和业务逻辑,不负责UI的呈现
  2. 带有内部状态
  3. 使用Redux的API

组件结构

  • 用容器组件包裹UI组件
  • 容器组件负责与外部的通信,将数据传给UI组件
  • UI组件渲染出视图
  • connect函数(来自react-redux插件):连接React组件与React store

关键字

  • connect
  • Provider

App修改为UI组件

  • index.js
import ReactDOM from 'react-dom'
import App from './App'
import { createStore } from 'redux'
import { counter } from './redux/reducers'
import { Provider } from 'react-redux'const store = createStore(counter)
ReactDOM.render(<Provider store={store}><App /></Provider>,document.getElementById('root')
)
  • App.jsx
import React, { Component, createRef } from 'react'
import { connect } from 'react-redux'
import * as actions from './redux/action'
class App extends Component {constructor(props) {super(props)this.selectRef = createRef()}compute = (method) => {const selectDom = this.selectRef.current,selectVal = Number(selectDom.value);// 使用传入的props.actions this.props[method](selectVal)}render() {// 将App改造为UI组件const { count } = this.propsconsole.log('【app】', this)return (<><h1>数值:{count}</h1><select ref={this.selectRef}><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>&nbsp;<button onClick={() => this.compute('add')}>+</button>&nbsp;<button onClick={() => this.compute('minus')}>-</button>&nbsp;<button onClick={() => this.compute('add_odd')}>奇数加</button>&nbsp;<button onClick={() => this.compute('add_delay')}>延迟加</button></>)}
}
// 用connect函数处理App再暴露出去
export default connect(state => ({count: state // 这里的state是从redux容器中获取到的数据}),{ ...actions }// 要解构出来!否则props里拿不到// 这样即可在App这个UI组件里通过this.props.actions来使用
)(App)

connect方法接收2个参数

  1. 函数mapStateToProps,建立state对象到props对象的映射关系
    (redux store里的state可以通过UI组件的props获取)
  2. mapDispatchToProps,建立一个store.dispatch方法到props对象的方法
    (redux里面action creators创建的函数可以通过props获取)

在这里插入图片描述

使用中间件

reducer:纯函数,只承担计算state的功能
view:与state意义对应
action:存放数据的对象,只能被别人操作?
同步:action发出后,reducer立即计算出state
异步:action发出后,过段时间再执行reducer
中间件:一个函数,对store.dispatch方法进行改造,在发出action和执行reducer两步之间添加了其他功能

  • index.js(引入中间件)
import ReactDOM from 'react-dom'
import App from './App'
// 引入中间件
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import { counter } from './redux/reducers'
import { Provider } from 'react-redux'const store = createStore(counter, applyMiddleware(thunk))
ReactDOM.render(<Provider store={store}><App /></Provider>,document.getElementById('root')
)
  • reducer.js(只有同步方法)
export function counter(state = 0, action) {const { type, data } = actionswitch (type) {case 'add':return state + datacase 'minus':return state - datacase 'add_odd':if (data % 2 !== 0) {return state + data}// 这里面没有异步方法default:return state}
}
  • action.js (增加异步方法)
export function add(param) {return {type: 'add', // 方法名data: param // 对应参数}
}
export function minus(param) {return {type: 'minus',data: param}
}
export function add_odd(param) {return {type: 'add_odd',data: param}
}
// 改造成异步action
export function add_delay(param) {return dispatch => {setTimeout(() => {dispatch(add(param))}, 1000)}
}

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

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

相关文章

PHP数组函数

整理了一份PHP开发中数组操作大全&#xff0c;包含有数组操作的基本函数、数组的分段和填充、数组与栈、数组与列队、回调函数、排序、计算、其他的数组函数等。 一、数组操作的基本函数 数组的键名和值 array_values($arr); 获得数组的值array_keys($arr); 获得数组的键名ar…

Android Stadio 相关

这几天&#xff0c;电脑坏了。重装系统&#xff0c;慢慢的学到了很多Android stadio 的相关知识。总结一下吧&#xff1a; 1.gradle 编译工具&#xff1a;在工程的gradle/wrapper/gradle–wrapper.properties 里面设置gradle 的版本。 distributionUrlhttps://services.grad…

1 State Hook

Hook&#xff0c;使用在函数组件中不要在循环&#xff0c;条件或嵌套函数中(if、switch、for)调用 Hook 1. 函数指向相同的引用 更新方式&#xff1a;函数组件中state变化时才重新渲染&#xff08;React使用Object.is比较算法来比较state&#xff09;&#xff1b;而类组件每次…

⊰第五篇⊱ 队列

队列&#xff08;queue&#xff09;是只允许在一端进行插入操作&#xff0c;而在另一端进行删除操作的线性表。 队列是一种先进先出的&#xff08;First In First Out&#xff09;的线性表&#xff0c;简称FIFO。允许插入的一端为队尾&#xff0c;允许删除的一端为队头。队列不…

es6 --- 数组的扩展

经常遇到对数组的操作…下面是《ES6标准入门》(第3版)中对数组扩展的(部分)描述: 扩展运算符(…): console.log(...[1,2,3]) // 1 2 3console.log(1, ... [2,3,4], 5) // 1 2 3 4 5扩展运算符代替数组的apply方法 // ES5 function f(x,y,z) {// ... } var args [1,2,3]; f.a…

算法 - 排序稳定性总结

排序方式 时间复杂度 空间复杂度 稳定性 平均情况 最坏情况 最好情况 插入排序 O(n^2) O(n^2) O(n) O(1) 稳定 希尔排序 O(n^1.3) O(1) 不稳定 冒泡排序 O(n^2) O(n^2) O(n) O(1) 稳定 快速排序 O(nlogn) O(n^2) O(nlogn) O(logn) 不稳定 选择排…

node --- 实践中理解跨域

经常可以见到.说解决跨域只要返回加上"Access-Control-Allow-Origin"头部就行… 下面从实践中一步一步的理解. 1.环境准备: 1. node.js (http://nodejs.cn/) 自行下载配置, 完毕后(cmd)输入 node --version 若显示版本号则代表成功// ps: node(中的npm)方便下载资源…

熟悉常用的Linux操作

cd命令&#xff1a;切换目录 &#xff08;1&#xff09; 切换到目录 /usr/local Cd /usr/local &#xff08;2&#xff09; 去到目前的上层目录 Cd .. &#xff08;3&#xff09;回到自己的主文件夹 Cd ~ ls命令&#xff1a;查看文件与目录 &#xff08;4&#xff09;查看…

2 Effect Hook

副作用&#xff1a;和外部有交互 引用外部变量调用外部函数修改dom、全局变量ajax计时器&#xff08;依赖window.setTimeout&#xff09;存储相关 纯函数&#xff1a;相同的输入一定会得到相同的输出 Effect Hook可以让你在函数组件中执行副作用操作 类组件中处理副作用 在com…

【JUC】CountDownLatch

因为在调用端的异步中&#xff0c;需要调用其他多个服务获取数据再汇总结果返回&#xff0c;所以用到了CountDownLatch CountDownLatch的概念 CountDownLatch是一个同步工具类&#xff0c;用来协调多个线程之间的同步&#xff0c;或者说起到线程之间的通信&#xff08;而不是用…

node --- Missing write access to 解决

今天在使用npm安装animate.css时报错… 大体原因是没有对node_modules没有写的权限. 百度查到是要删除对应的node_modules然后在安装… 但是我并不想这样做…想起前面我为了加快下载速度,好像使用的是cnpm… 于是我使用了nrm ls 查看当前使用的源 更换npm的源可以参考 https:…

3 useReducer及其实现

pureComponent import { useState } from "react" // useReducer, // 统一调度 function reducer(state, action) {console.log(reducer接收参数, state, action)const { type } actionswitch (type) {case add:return { num: state.num 1 }case minus:return { n…

Django 之 权限系统(组件)

参考: http://www.cnblogs.com/yuanchenqi/articles/7609586.html 转载于:https://www.cnblogs.com/bigtreei/p/8564243.html

vue踩坑- 报错npm ERR! cb() never called!

在vue项目中引入饿了么elementUI组件的步骤之中&#xff0c;出现以下的错误&#xff1a; D:\my-project-first>npm i element-ui -S Unhandled rejection RangeError: Maximum call stack size exceededill install loadIdealTreeat RegExp.test (<anonymous>)at D:\n…

maven之阿里云Maven镜像的使用

Maven中央仓库在国外&#xff0c;速度比较慢&#xff0c;所以我们采用国内的镜像&#xff0c;速度回有质的提升。 配置下setting.xml <mirrors><mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/ne…

vue --- 使用animate.css实现动画

1.下载animate.css npm install --save-dev animate.css// 注意你使用的源 nrm ls(若没有改变可以忽略)2.导入animate.css <link rel"stylesheet" href"../node_modules/animate.css/animate.css"> // 注意你的当前文件和node_moudules文件夹的相对…

4 contextHook

类组件createContext、静态属性contextType 与函数组件useContext 的对比 import { Component, createContext, useContext } from react const AppContext createContext(0) class Foo extends Component {render() {return (<AppContext.Consumer>{value > (Foo: …

【leetcode 简单】 第一百一十题 分发饼干

假设你是一位很棒的家长&#xff0c;想要给你的孩子们一些小饼干。但是&#xff0c;每个孩子最多只能给一块饼干。对每个孩子 i &#xff0c;都有一个胃口值 gi &#xff0c;这是能让孩子们满足胃口的饼干的最小尺寸&#xff1b;并且每块饼干 j &#xff0c;都有一个尺寸 sj 。…

基于openstack搭建百万级并发负载均衡器的解决方案

最近&#xff0c;喜欢研究一些国外技术大咖们的文章&#xff0c;而这篇文章是基于openstack负载均衡器的解决方案&#xff0c;做的一些总结~希望能够给小伙伴带来一些灵感或者帮助。 openstack现有的负载均衡解决方案&#xff0c;无论是lbaas plugin还是octavia&#xff0c;后端…

5 useMemouseCallback

useMemo 优化渲染 现象 App每次重新执行时&#xff0c;render变化了&#xff0c;引用的render不是同一个函数 import React, { useState, } from "react"; const Foo props > {return <ul>{props.render()}</ul> } function App() {const [range…