Bor*_*ald 1 reactjs redux redux-toolkit
我有一个反应应用程序,在初始加载时会执行大量 fetch 调用来检索一些数据。
我使用 redux 和 redux 工具包来管理状态
正如您所看到的,在我的 redux 切片中,有很多链接的 addCase 调用,它们只是处理设置加载状态以获取loading或failed
有没有办法避免这种情况并使我的代码更简洁?一种制作单个 addCase 的方法,该 addCase 处理将加载状态设置loading为所有获取以及同样的情况failed?
export const dashboardSlice = createSlice({
name: "dashboard",
initialState,
reducers: {},
extraReducers(builder) {
builder
//Co2Emission
.addCase(fetchCO2EmissionData.pending, (state, action) => {
state.co2.status = "loading";
})
.addCase(fetchCO2EmissionData.fulfilled, (state, action) => {
state.co2.status = "succeeded";
//We are just using the CPU data from first pod in the array. When when KG-121 it should just be state.Co2DiagramData = action.payload.values
console.log(action.payload[0]);
const transformedData = action.payload[0].values.map(d=>({Date: convertDate(d[0]*1000),"Grams of CO2": parseFloat(d[1])}));
state.co2.data = transformedData;
})
.addCase(fetchCO2EmissionData.rejected, (state, action) => {
state.co2.status = "failed";
})
//Active pods
.addCase(fetchActivePods.pending, (state, action) => {
state.pods.status = "loading";
})
.addCase(fetchActivePods.fulfilled, (state, action) => {
state.pods.status = "succeeded";
state.pods.currentValue = parseFloat(action.payload[0].values.pop().pop());
state.pods.data = action.payload[0].values;
})
.addCase(fetchActivePods.rejected, (state, action) => {
state.pods.status = "failed";
})
//Cpu usage
.addCase(fetchCpuUsage.pending, (state, action) => {
state.cpu.statusUsage = 'loading'
})
.addCase(fetchCpuUsage.fulfilled, (state, action) => {
state.cpu.statusUsage = 'succeeded';
state.cpu.currentUsage = parseFloat(action.payload[0].values.pop().pop());
state.cpu.usage = action.payload[0].values;
})
.addCase(fetchCpuUsage.rejected, (state, action) => {
state.cpu.statusUsage = 'failed'
})
//cpu allocation
.addCase(fetchCpuAllocation.pending, (state, action) => {
state.cpu.statusAllocation = 'loading'
})
.addCase(fetchCpuAllocation.fulfilled, (state, action) => {
state.cpu.statusAllocation = 'succeeded';
if(action.payload.values.length !== 0){
state.cpu.currentAllocated = parseFloat(action.payload[0].values.pop().pop());
state.cpu.allocated = action.payload[0].values;
}
})
.addCase(fetchCpuAllocation.rejected, (state, action) => {
state.cpu.statusAllocation = 'failed'
})
//memory usage
.addCase(fetchMemoryUsage.pending, (state, action) => {
state.memory.statusUsage = 'loading'
})
.addCase(fetchMemoryUsage.fulfilled, (state, action) => {
state.memory.statusUsage = 'succeeded';
state.memory.currentUsage = parseFloat(action.payload[0].values.pop().pop());
state.memory.usage = action.payload[0].values;
})
.addCase(fetchMemoryUsage.rejected, (state, action) => {
state.memory.statusUsage = 'failed'
})
//memory allocation
.addCase(fetchMemoryAllocation.pending, (state, action) => {
state.cpu.statusAllocation = 'loading'
})
.addCase(fetchMemoryAllocation.fulfilled, (state, action) => {
state.memory.statusAllocation = 'succeeded';
if(action.payload[0].values.length !== 0){
state.memory.currentAllocated = parseFloat(action.payload[0].values.pop().pop());
state.memory.allocated = action.payload[0].values;
}
})
.addCase(fetchMemoryAllocation.rejected, (state, action) => {
state.memory.statusAllocation = 'failed'
})
}
})
export default dashboardSlice.reducer;
Run Code Online (Sandbox Code Playgroud)
您可能应该看看 RTK Query,它也是 Redux Toolkit 的一部分,并在内部处理所有这些内容 - 您只需描述您的 API,它就会自动为您生成减速器、中间件甚至钩子。无需手动处理加载状态。
官方 Redux 教程中对此进行了介绍
| 归档时间: |
|
| 查看次数: |
9320 次 |
| 最近记录: |