如何修复.push不是redux-toolkit中的函数[已关闭]

c9qzyr3d  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(224)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
5天前关闭。
Improve this question
如何修复状态。push不是redux-toolkit中的函数
当我试图添加一个新的techStackList的状态,我得到“state.techStackList.push不是一个函数”错误消息如何解决这个问题

export const techStackDetail = createSlice({
  name: 'techStackDetail',
  initialState: {
    techStackList: [],
    loading: false,
    error: null,
  },
  reducers: {},
  extraReducers: {
    [createTechStack.pending]: state => {
      state.loading = true
    },
    [createTechStack.fulfilled]: (state, action) => {
      state.loading = false;
      state.techStackList.push(action?.payload);
    },
    [createTechStack.rejected]: (state, action) => {
      state.loading = false
      state.error = action.payload.message
    },
  },
})
fnvucqvd

fnvucqvd1#

push()方法不返回数组。它会改变数组,这在Redux中是不应该做的
试试这个:

export const techStackDetail = createSlice({
  name: 'techStackDetail',
  initialState: {
    techStackList: [],
    loading: false,
    error: null,
  },
  reducers: {},
  extraReducers: {
    [createTechStack.pending]: state => {
      state.loading = true;
    },
    [createTechStack.fulfilled]: (state, action) => {
      state.loading = false;
      state.techStackList = [...state.techStackList, action?.payload];
    },
    [createTechStack.rejected]: (state, action) => {
      state.loading = false;
      state.error = action.payload.message;
    },
  },
});

相关问题