简体   繁体   English

Redux-toolkit:state 是 createSlice reducer 中的代理

[英]Redux-toolkit: state is a proxy inside a createSlice reducer

I am trying to append / update some data in a state array inside of my slice reducers, but when I try to console.log the .projects array of the state that I am interested in I just get a javascript Proxy.我正在尝试在切片缩减器内部的状态数组中附加/更新一些数据,但是当我尝试console.log我感兴趣的状态的.projects数组时,我只得到了一个 javascript 代理。 What is going on here (what am I doing wrong)?这里发生了什么(我做错了什么)?

import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  projects: [],
};

const projectsSlice = createSlice({
  name: 'projectsSlice',
  initialState: { ...initialState },
  reducers: {
    addProject(state, action) {
      const { project } = action.payload;
      const newProjects = [project, ...state.projects];

      console.group('add project');
      console.log('project: ', project);
      console.log('state projects: ', state.projects);
      console.log('newProjects: ', newProjects);
      console.groupEnd();

      state.projects = newProjects;
    },
    setProjects(state, action) {
      const { projects } = action.payload;
      state.projects = projects;
    },
    removeProject(state, action) {
      const { projectId } = action.payload;
      const newProjects = [...state.projects].filter((project) => project.id !== projectId);
      state.projects = newProjects;
    },
    updateProject(state, action) {
      const { project } = action.payload;
      const projectIndex = state.projects.findIndex((stateProject) => stateProject.id === project.id);
      const newProjects = [...state.projects].splice(projectIndex, 1, project);

      console.group('updateProject');
      console.log('project: ', project);
      console.log('projectIndex: ', projectIndex);
      console.log('state projects: ', state.projects);
      console.log('newProjects: ', newProjects);
      console.groupEnd();

      state.projects = newProjects;
    },
  },
});

export const { addProject, removeProject, updateProject, setProjects } = projectsSlice.actions;

export default projectsSlice.reducer;

The Proxy there is the reason you can just mutate state in that reducer and just get an immutable copy in your state - but browsers are really bad at logging proxies.代理是您可以改变该减速器中的状态并在您的状态中获得不可变副本的原因 - 但浏览器在记录代理方面非常糟糕。

per the docs , you can use the current export of RTK&immer: 根据文档,您可以使用 RTK&immer 的current导出:

const slice = createSlice({
  name: 'todos',
  initialState: [{ id: 1, title: 'Example todo' }],
  reducers: {
    addTodo: (state, action) => {
      console.log('before', current(state))
      state.push(action.payload)
      console.log('after', current(state))
    },
  },
})

You need to use current您需要使用当前

import { current } from '@reduxjs/toolkit'

and with that, you can reach the current state and work with them and after that, you can send a return in reducer to return new data.这样,您就可以达到当前状态并使用它们,之后,您可以在 reducer 中发送 return 以返回新数据。

Will looks like this:将看起来像这样:

const referralSlicer = createSlice({
name: 'referral',
initialState: {
  referrals: [],
  currentCard: 0,
} as IReferralSlicer,
reducers: {
  addReferrals(state, { payload }) {
    return {
      referrals: [...state.referrals, payload],
    }
  },
  deleteReferral(state, { payload }) {
    const currentState = current(state)

    return {
      ...currentState,
      referrals: currentState.referrals.splice(payload, 1),
    }
  },
  setCurrentCard(state, { payload }) {
    return {
      referrals: state.referrals,
      currentCard: payload,
    }
  },
},
})

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

相关问题 如何使用 redux-toolkit 中的 createSlice 方法在不同的减速器 function 中使用单动作类型 - How to use single action type in different reducer function with createSlice method in redux-toolkit 在 reducer 中未触发 redux-toolkit 操作 - redux-toolkit action not triggered in reducer “Unexpected key ”foo“ found in previous state received by the reducer” 使用 redux-toolkit 配置存储和自定义存储增强器 - “Unexpected key ”foo“ found in previous state received by the reducer” with redux-toolkit configure store and a custom store enhancer 如何使用 redux-toolkit 在另一个切片的 reducer 中访问一个切片的状态 - How to access state of one slice in reducer of another slice using redux-toolkit Redux 工具包:在 reducer 中显示为 Proxy / undefined 的状态 - Redux Toolkit: state showing as Proxy / undefined within reducer 如何在 redux-toolkit 中观察状态变量? - How to watch state variables in redux-toolkit? 使用 redux-toolkit 将 state 重置为初始值 - Reset state to initial with redux-toolkit Redux 工具包在 React 中创建切片 - Redux Toolkit createSlice in React 使用 redux-toolkit 时商店没有有效的 reducer - Store does not have a valid reducer when using redux-toolkit 使用 createSlice 中的 reducer 从 redux 状态的数组中删除元素 - Delete element from array in redux state using a reducer in createSlice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM