无法在 redux 工具包中附加化简器状态



我想用action.payload附加state.data,我已经尝试了它在.concat、.push或数组中扩展时提供的所有内容。

import { createAction, createReducer } from "@reduxjs/toolkit";
const initialState = {};
const request = createAction("allDataRequest");
const success = createAction("allDataSuccess");
const fail = createAction("allDataFailure");
const clear = createAction("clearErrors");

export const allServicesReducer = createReducer(initialState, (builder) => {
builder
.addCase(request, (state, action) => {
state.loading = true;
})
.addCase(success, (state, action) => {
state.loading = false;
state.data = action.payload;
// I want to append this state.data with payload
//state.data = state.data.concat(action.payload)
//state.data = [...state.data, ...action.payload]
// However i get stae.data undefined on both
})
.addCase(fail, (state, action) => {
state.loading = false;
state.error = action.payload;
})
.addCase(clear, (state, action) => {
state.error = null;
});
});

这是因为您的初始状态没有.data字段

const initialState = {};

所以,是的,一开始它将是空的。

您需要提供const initialState = {data: []},使字段,或者更新reducer逻辑以处理不存在的情况。

相关内容

  • 没有找到相关文章

最新更新