網站首頁 編程語言 正文
1、Redux-thunk中間件
第一步 安裝redux-thunk中間件
npm install redux-thunk
第二步 在store中引入thunk組件
import {createStore,applyMiddleware } from 'redux';
import Reducer from './Reducer';
import thunk from 'redux-thunk';
const store = createStore(Reducer,applyMiddleware(thunk));
export default store;
第三步 封裝異步請求方法
在TodoList.js組件中,封裝異步獲取請求的方法:
import React, { Component } from 'react'
import Store from './Store'
import axios from 'axios'
export class TodoList extends Component {
constructor(props){
super(props);
this.state = Store.getState();
this.handleStoreChange = this.handleStoreChange.bind(this);
Store.subscribe(this.handleStoreChange);
}
//在生命周期函數中調用異步方法
componentDidMount(){
Store.dispatch(this.getTodoListDatas());
}
//異步獲取請求的方法
getTodoListDatas(){
return (dispatch)=>{
axios.get("/data.json")
.then(resp => {
const action = {
type:'axios_getdata',
data:resp.data
}
dispatch(action)
})
}
}
handleStoreChange(){
this.setState(Store.getState());
}
render() {
return (
<div>
<input type='text' value={this.state.inputValue}></input>
<button>添加</button>
<hr></hr>
<ul>
{this.state.list.map((item,index)=>{
return (
<li key={index}>{item}</li>
);
})}
</ul>
</div>
)
}
}
export default TodoList
第四步 在reducer中接收action信息
const defaultState = {
inputValue:'',
list:[]
}
export default (state = defaultState,action) => {
if(action.type === 'axios_getdata'){
const newState = state;
newState.list = action.data;
return newState;
}
return state
}
2、Redux中間件執行原理
redux-thunk這個中間件可以使我們把這樣的異步請求或者說復雜的邏輯可以放到action里面去處理,redux-thunk使redux的一個中間件,為什么叫做中間件
我們說中間件,那么肯定是誰和誰的中間,那么redux的中間件指的是誰和誰的中間呢?
如圖。view在redux中會派發一個action,action通過store的dispatch方法派發給store,store接收到action,連同之前到state,一起傳給reducer,reducer返回一個新到數據給store,store去改變自己到state。這是redux的一個標準流程,那么我們說redux的中間件指的是誰和誰的之間,大家一定要記住, redux的中間件指的是action和store之間。之前我們說action只能是一個對象,所以action是一個對象直接派發給了store。 但是現在,當我們使用了redux-thunk之后,action可以是函數了。
為什么可以是函數呢,看這張圖。action通過dispatch方法被傳遞給store,那么action和store之間是誰,是不是就是dispatch這個方法, 實際上我們指的中間件指的是什么呢,就是對dispatch方法的一個封裝,或者說是dispatch方法的一個升級,最原始的dispatch方法,他接收到一個對象之后,會把這個對象傳遞給store,這就是view中間件的一個情況。 當我們對dispath做了一個升級之后,比如說我們使用了redux-thunk這個中間件,對dispath做了一個升級,這個時候當你調用dispatch方法,給dispatch傳遞的參數是一個對象的話,那么這個dispatch就會把這個對象直接傳給store。跟之前寫一個對象,調用dispatch傳給store沒有任何的區別。但是這個時候假設傳給dispatch方法是一個函數的話,這個時候dispatch方法已經升級了。他知道如果你傳遞過來是一個函數的話,他就不會把這個函數直接傳遞給store。他會怎么辦呢?
他會讓你這個函數先執行,然后執行完了之后,需要調用store的時候,這個函數再去調用store。所以dispatch做了一個事情,他會根據參數的不同,執行不同的事情,如果你參數是對象,那我直接傳給store。如果你參數是函數,那就把這個函數執行結束。所以講到這大家應該知道
redux中間件,他的原理是非常簡單的,他就是對store對dispatch方法做一個升級,之前這個dispatch方法只能接收一個對象,現在升級之后,就可以接收對象,也可以接收函數了。 當然這里用什么對他進行一個升級呢?用redux-thunk對他進行了升級。當然中間件不只redux-thunk這一個,實際上redux中間件非常多,比如說我們說的redux-log,可以記錄action每次派發的日志,那他怎么記錄呢?其實也很簡單,每次調用 action的時候,都會通過dispatch把這個action傳遞給store,那么我可以對dispatch做一個升級,dispatch不僅僅把action傳遞給store,而且在每次傳遞之前呢,還通過console.log,把這個action打印出來。這樣就寫了一個redux-log的中間件。他可以在每次派發action的時候,把這個action打印在控制臺里面。
最近用的比較火的redux中間件,除了redux-thunk,redux-log這樣的東西,還有一個中間件,叫做redux-saga。他的應用范圍也非常廣,redux-saga也是解決redux中間異步問題的中間件。不同于redux-thunk。redux-thunk是把異步操作放在action里面操作。而redux-saga采用的設計思想是,單獨的把一個異步邏輯拆分出來,放在一個異步文件里面管理,基本上掌握了redux-thunk和redux-saga這兩者的設計思路之后呢,再去做redux里面的異步邏輯,或者說復雜的邏輯,如何去拆分,就比較明白了。
原文鏈接:https://blog.csdn.net/p445098355/article/details/105218217
相關推薦
- 2022-04-15 C語言?使用qsort函數來進行快速排序_C 語言
- 2022-02-26 sparksql之通過 structType 創建 DataFrames(編程接口)
- 2023-05-06 如何設置docker開機自啟動,并設置容器自動重啟_docker
- 2022-04-14 Python函數式編程實現登錄注冊功能_python
- 2022-12-12 Android?DataBinding類關系深入探究_Android
- 2022-09-22 git-lfs 離線安裝
- 2022-08-23 Python快速從視頻中提取視頻幀的方法詳解_python
- 2022-05-31 openCV顯著性檢測的使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支