【发布时间】:2022-10-02 10:14:00
【问题描述】:
我想用 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;
});
});
标签: react-native redux react-redux redux-toolkit