当前位置:网站首页>解决reudx中的异步问题 applyMiddleware thunk
解决reudx中的异步问题 applyMiddleware thunk
2022-07-29 20:47:00 【敢问】
applyMiddleware
redux中的异步解决方案

component中通过dispatch传递action到store中,store通过middleware处理一些功能(异步功能),传递action到reducer,又会返回新的state到store中,store中的state发生改变会触发subscribe来对component重新渲染
redux整个流程本身事同步的
middleware API 来自于redux。redux/applyMiddleware.md at master · reduxjs/redux · GitHub
- 引入方式 import {createStore,applyMiddleware} from 'redux'
- 执行位置 let store = createStore( reducer ,applyMiddleware(test,logger))
- 参数是自定义中间件
- 中间件函数的写法 是 固定不变的
- 每个中间件函数内部必须调用next()
- 中间件函数执行的顺序跟在applyMiddleware() 中的参数有关
let store = createStore( reducer ,applyMiddleware(test,logger)) - 虽然中间件可以拓展redux中的应用程序,可以添加异步代码,但是会造成所有的action都会出现异步功能,这不是想要的结果,中间件只是告诉我们, 谁(action)是异步的,谁不是异步的
中间件src/store/middleware/logger.js
// function logger({ getState }) {
// return next => action => {
// const returnValue = next(action)
// return returnValue
// }
// }
export function logger(store){
return function(next){
return function(action){
console.log('1111')
next(action)
}
}
}
// 添加异步代码
export function logger(store){
return function(next){
return function(action){
let t = setTimeout(() => {
console.log('1111')
},2000)
next(action)
}
}
}中间件 src/store/middleware/test.js
export function test(store){
return function(next){
return function(action){
console.log('2222')
next(action)
}
}
}store/index.js
import {createStore,applyMiddleware} from 'redux'
import reducer from './reducer'
import {logger} from './middleware/logger'
import {test} from './middleware/test'
//1. 定义store
let store = createStore( reducer ,applyMiddleware(test,logger))
export default store redux-thunk. 中间件解决异步的方法
将异步代码写在 action中,中间件thunk只是告诉我们,谁(action)是异步谁不是
action 返回的是一个函数而不是一个对象
action.js
// 4. 定义action
// let increment = {type:'increment'}
// let decrement = {type:'decrement'}
// export function increment(){
// return{
// type:'increment'
// }
// }
// export function decrement(){
// return{
// type:'decrement'
// }
// }
export const increment = (num) => ({ type:'increment',payload:num }) // 同步action
// export const incrementasync = function(num){
// return function(dispatch){
// let t = setTimeout(() => {
// dispatch(increment(num))
// },2000)
// }
// }
export const incrementasync = (num) => (dispatch) => { //异步action
let t = setTimeout(() => {
dispatch(increment(num))
},2000)
}
export const decrement = (num) => ({ type:'decrement',payload:num })
export const fngreen = () => ({ type:'fngreen'})
export const fnred = () => ({ type:'fnred' })中间件thunk.js
export function thunk(store){
return function(next){
return function(action){
if(typeof action === 'function'){
action(store.dispatch)
}else{
next(action)
}
}
}
}边栏推荐
- Dry goods!Cooperative Balance in Federated Learning
- 容器网络硬核技术内幕 (小结-中)
- First thoughts on the first attempt to avoid killing without a file (Part 1)
- 人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
- 336. Palindromic Pairs
- offsetwidth111[easy to understand]
- json-c实现json和结构体之间的相互转换
- 断言+异常处理类,代码更简洁了
- 品牌广告投放平台的中台化应用与实践
- Fully automated machine learning modeling!The effect hangs the primary alchemist!
猜你喜欢

MySQL数据查询 - 联合查询

leetcode-593:有效的正方形

SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南试读版

OneNote 教程,如何在 OneNote 中做笔记?

品牌广告投放平台的中台化应用与实践

TCP协议详解

APM电机输出逻辑(Motors类详解)

4. Implementation Guide for GET_ENTITYSET Method of SAP ABAP OData Service Data Provider Class

The younger brother asked: Is the work of a programmer a day’s work of code?

Cooler Navigation helps you shop easily in shopping malls without confusion
随机推荐
4. Implementation Guide for GET_ENTITYSET Method of SAP ABAP OData Service Data Provider Class
240. Searching 2D Matrix II
分布式限流 redission RRateLimiter 的使用及原理
容器网络硬核技术内幕 (小结-下)
南华早报 | 助力亚洲最具公信力报章实现AD域自动化管理
容器网络硬核技术内幕 (小结-中)
json-c实现json和结构体之间的相互转换
一 JS中Promise用法、二闭包的概念与用法、三对象创建的四种方式与区区别、四 如何声明一个类
The Ministry of Human Resources and Social Security announced that "database operation administrator" has become a new occupation, and OceanBase participated in the formulation of occupational standar
惠普服务器硬盘指示灯不亮或显示蓝色
[ACTF2020 Freshman Competition]Exec 1
LeetCode 0593. 有效的正方形
Qualcomm WLAN framework learning (31) -- Power save
OneNote tutorial, how to take notes in OneNote?
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
带你刷(牛客网)C语言百题(第四天)
分析少年派2中的Crypto
Baidu internship students late night fun: originally giant is this kind of life
Dry goods!Cooperative Balance in Federated Learning
LeetCode--单链表--146.LRU缓存