redux 工具包切片状态不清除

redux toolkit slice state doesn't clear

我想用 _resetNewAdverbController reducer 清除(重置)这些状态,我做到了。我在保存数据后发送它并且发送工作正常我检查它。但数据仍然存在。 如何清除它以保存另一个新数据?

这是我的切片,用于保存一些信息。

import { citiesType, subCatType, uploadImagesType } from "./../../types/types";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { newAdverbControllerState } from "../../initialStates/newAdverbController";

const initialState: newAdverbControllerState = {
  level: 1,
  catData: {
    subId: "",
    subTitle: "",
  },
  cityData: {
    cityId: "",
    cityName: "",
  },
  around: "",
  title: "",
  description: "",
  suggests: [],
  deleteSuggest: 0,
  phoneNumber: "",
  showPhoneNumber: false,
  chatEnabled: false,
  uploadImages: [],
};

export const newAdverbControllerSlice = createSlice({
  name: "newAdverbController",
  initialState,
  reducers: {
    setLevel: (state: { level: number }, action: PayloadAction<number>) => {
      state.level = action.payload;
    },
    setSubData: (
      state: { catData: subCatType },
      action: PayloadAction<subCatType>
    ) => {
      state.catData = action.payload;
    },
    setCityData: (
      state: { cityData: citiesType },
      action: PayloadAction<citiesType>
    ) => {
      state.cityData = action.payload;
    },
    _setAround: (state, action: PayloadAction<string>) => {
      state.around = action.payload;
    },
    _setTitle: (state, action: PayloadAction<string>) => {
      state.title = action.payload;
    },
    _setDescription: (
      state: { description: string },
      action: PayloadAction<string>
    ) => {
      state.description = action.payload;
    },
    _setSuggest: (
      state: { suggests: string[] },
      action: PayloadAction<string>
    ) => {
      state.suggests.push(action.payload);
    },
    _deleteSuggest: (
      state: { suggests: any[] },
      action: PayloadAction<number>
    ) => {
      state.suggests.splice(action.payload, 1);
    },
    _setPhoneNumber: (
      state: { phoneNumber: string },
      action: PayloadAction<string>
    ) => {
      state.phoneNumber = action.payload;
    },
    _setShowPhoneNumber: (
      state: { showPhoneNumber: boolean },
      action: PayloadAction<boolean>
    ) => {
      state.showPhoneNumber = action.payload;
    },
    _setChatEnabled: (
      state: { chatEnabled: boolean },
      action: PayloadAction<boolean>
    ) => {
      state.chatEnabled = action.payload;
    },
    _setUploadImage: (
      state: { uploadImages: uploadImagesType[] },
      action: PayloadAction<uploadImagesType>
    ) => {
      state.uploadImages.push(action.payload);
    },
    _deleteUploadImage: (
      state: { uploadImages: any[] },
      action: PayloadAction<number>
    ) => {
      state.uploadImages.splice(action.payload, 1);
    },
    _setUploadImageState: (
      state: { uploadImages: { completed: boolean }[] },
      action: PayloadAction<number>
    ) => {
      state.uploadImages[action.payload].completed = true;
    },
    _resetNewAdverbController: (state: newAdverbControllerState) => {
      state = initialState;
    },
  },
});

export default newAdverbControllerSlice.reducer;
export const {
  setLevel,
  setSubData,
  setCityData,
  _setAround,
  _setTitle,
  _setDescription,
  _setSuggest,
  _deleteSuggest,
  _setPhoneNumber,
  _setShowPhoneNumber,
  _setChatEnabled,
  _setUploadImage,
  _setUploadImageState,
  _deleteUploadImage,
  _resetNewAdverbController,
} = newAdverbControllerSlice.actions;

我还在 _resetNewAdverbController 上设置了 undefined,就像这样 state = undefined; 但同样的问题;

return initialState,不是state = initialState

虽然您可以修改变量 state 中的对象(RTK 会观察到这些修改),但您不能重新分配变量 state,因为无法从外面。

另见 Resetting and Replacing State