web前端全系列 教程
1839个小节阅读:2243.9k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
提示:
createAsyncThunk
自动生成pending/fulfilled/rejected
action 类型
xxxxxxxxxx
//slices/productSlice.js
//创建获取商品数据的thunk
export const productListGet = createAsyncThunk('product/productListGet', async () => {
const data = await new Promise((resolve, reject) => {
setTimeout(() => {
resolve([{ name: '苹果' }, { name: '香蕉' }, { name: "蓝莓" }])
}, 3000)
})
return data
})
extraReducers(builder) {
builder
.addCase(addProductPost.pending, (state, action) => {
state.isLoading = true
})
.addCase(addProductPost.fulfilled, (state, action) => {
state.isLoading = false
state.list.push(action.payload)
})
.addCase(productListGet.pending, (state, action) => {
state.isLoading = true
})
.addCase(productListGet.fulfilled, (state, action) => {
return { list: action.payload, isLoading: false }
})
.addCase(productListGet.rejected, (state, action) => {
state.isLoading=false
})
}
xxxxxxxxxx
//pages/Product.js
import { addProductPost, productListGet } from '../slices/productSlice'
//组件挂载后请求商品数据
useEffect(() => {
dispatch(productListGet())
}, [])
提示:
Immer 让我们以两种方式更新状态:要么 更新 现有状态值,要么 return 一个新结果。
如果我们返回一个新值,它将用我们返回的任何内容完全替换现有状态。