web前端全系列 教程
1839个小节阅读:2243.7k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
thunk 是一种特定类型的 Redux 函数,可以包含异步逻辑。一个内部 thunk 函数,它以 dispatch
和 getState
作为参数
安装 redux-thunk
xxxxxxxxxx
npm install --save redux-thunk
引入 redux-thunk
xxxxxxxxxx
import { configureStore,applyMiddleware } from "@reduxjs/toolkit"
import { userReducer } from "./reducers/userReducers"
import { countReducer } from "./reducers/countReducers"
import thunk from "redux-thunk"
const store = configureStore({
reducer:{
user:userReducer,
count:countReducer
}
},applyMiddleware(thunk))
export default store
redux-thunk
可以在 actions
中实现
xxxxxxxxxx
export const asyncIncrementAction = (dispatch,getState) =>{
setTimeout(() =>{
dispatch(incrementAction())
console.log(getState())
},2000)
}
像使用普通 action creator 一样使用:
xxxxxxxxxx
dispatch(asyncIncrementAction)
如果需要传递参数,可以如下实现
xxxxxxxxxx
export const asyncIncrementAction = payload => (dispatch, getState) => {
setTimeout(() => {
console.log(payload)
dispatch(incrementAction())
}, 2000)
}
调用传递参数
xxxxxxxxxx
dispatch(asyncIncrementAction(10))