简体   繁体   English

Redux 工具包使用 Typescript 没有 state 突变

[英]Redux-toolkit usage with Typescript without state mutation

I'm working on a React project which is using Redux-Toolkit in JavaScript. I'm trying to shift project to TypeScript for debugging ease and Type Safety benefits.我正在开发一个在 JavaScript 中使用Redux-Toolkit的 React 项目。我正在尝试将项目转移到 TypeScript 以获得调试便利和类型安全优势。 My Slices code is as我的切片代码是

export const entitiesSlice = createSlice({
    name: "entities",
    initialState: initialentitiesState,
    reducers: {
      // getentityById
      entityFetched: (state, action) => {
        state.actionsLoading = false;
        state.entityForEdit = action.payload.entityForEdit;
        state.error = null;
      },
      // findentities
      entitiesFetched: (state, action) => {
        const { totalCount, entities } = action.payload;
        state.listLoading = false;
        state.error = null;
        state.entities = entities;
        state.totalCount = totalCount;
      },
      // createentity
      entityCreated: (state, action) => {
        state.actionsLoading = false;
        state.error = null;
        state.entities.push(action.payload.entity);
      },
      // updateentity
      entityUpdated: (state, action) => {
        state.error = null;
        state.actionsLoading = false;
        state.entities = state.entities.map(entity => {
          if (entity.id === action.payload.entity.id) {
            return action.payload.entity;
          }
          return entity;
        });
      },
      // deleteentities
      entitiesDeleted: (state, action) => {
        state.error = null;
        state.actionsLoading = false;
        state.entities = state.entities.filter(
          el => !action.payload.ids.includes(el.id)
        );
      },
      }
    }
  });
  

But I think the assignment like this state.somevar=updatedval is doing state mutation which is not good.但我认为像这样的分配state.somevar=updatedval正在做 state 突变这不好。 I want to declare my state Interface with readonly to avoid state mutation.我想用 readonly 声明我的 state 接口以避免 state 突变。 I have gone through the Redux-Toolkit-Usage-With-Typescript , which I think should avoid state mutation but all code snippets seem to be doing state mutation.我已经完成了Redux-Toolkit-Usage-With-Typescript ,我认为它应该避免 state 突变,但所有代码片段似乎都在做 state 突变。 I want something like this我想要这样的东西

      entityFetched: (state, action) => {
        return {
          ...state,
          actionsLoading:false,
          entityForEdit:action.payload.entityForEdit,
          error:null
        }
      }

Please guide me if I'm missing something or misinterpretting the meaning of state mutation.如果我遗漏了什么或误解了 state 突变的含义,请指导我。 Any broader advice for using TypeScript with React would be most welcomed!任何关于将 TypeScript 与 React 一起使用的更广泛的建议将是最受欢迎的! Thanks a Lot!多谢!

Redux Toolkit's createReducer and createSlice APIs use the Immer library internally, which allows you to write "mutating" syntax in your reducers, but turns that into safe and correct immutably updates. Redux Toolkit 的createReducercreateSlice API 在内部使用Immer 库,它允许您在 reducer 中编写“可变”语法,但将其转换为安全且正确的不变更新。

Please read through the new "Redux Essentials" core docs tutorial for further explanations on how Redux relies on immutability, and how Immer makes it safe to write "mutations" in your reducers.请通读新的“Redux Essentials”核心文档教程,以进一步解释 Redux 如何依赖不变性,以及 Immer 如何确保在 reducer 中安全地编写“突变”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM