Redux如何在状态中插入新的子数据

发布于 2025-02-11 06:11:35 字数 1062 浏览 2 评论 0原文

我正在制作一个todo应用,并使用redux进行状态管理。我的待办事项由嵌套阵列组成。

const initialState = {
  todos: [
    {
      id: 1,
      name: "task1",
      subdata: [
        {
          id: 101,
          name: "subtask1",
          complete: false,
        },
        {
          id: 102,
          name: "subtask2",
          complete: true,
        },
      ],
    },
    {
      id: 2,
      name: "task2",
      subdata: [
        {
          id: 103,
          name: "subtask3",
          complete: false,
        },
        {
          id: 104,
          name: "subtask4",
          complete: true,
        },
      ],
    },

还原器:

export default function reducer(state = initialState, action) {
  switch (action.type) {
    case ADD_TODO:
      const newTodo = state.todos.concat(action.payload);
      return { ...state, todos: newTodo };

    case ADD_SUBTODO:
      const newSubtodo = action.payload;
?????????????????????????????????????????????

如何将新的Subtodo附加到初始状态? 我使用了沉浸库,但我想以传统的方式进行操作,例如传播操作员。如果您提供帮助,我会很高兴。

I'm making a todo app and using redux for state management. My todo state is made up of nested arrays.

const initialState = {
  todos: [
    {
      id: 1,
      name: "task1",
      subdata: [
        {
          id: 101,
          name: "subtask1",
          complete: false,
        },
        {
          id: 102,
          name: "subtask2",
          complete: true,
        },
      ],
    },
    {
      id: 2,
      name: "task2",
      subdata: [
        {
          id: 103,
          name: "subtask3",
          complete: false,
        },
        {
          id: 104,
          name: "subtask4",
          complete: true,
        },
      ],
    },

Reducers:

export default function reducer(state = initialState, action) {
  switch (action.type) {
    case ADD_TODO:
      const newTodo = state.todos.concat(action.payload);
      return { ...state, todos: newTodo };

    case ADD_SUBTODO:
      const newSubtodo = action.payload;
?????????????????????????????????????????????

How can i append new subtodo to initialstate?
I used the immer library, but I want to do it the traditional way, for example the spread operator. I would be glad if you help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2025-02-18 06:11:35

你可以做类似...

// send payload as {id:1,newSubtodo: newSubtodo} 
case ADD_SUBTODO:
      const newSubtodo = action.payload.newSubtodo;
      //id is the task/todo id of which you want to add a new subdata
      const newTask = initialState.todos.find(i=>i.id==action.payload.id) 
      //id is the task/todo id of which you want to add a new subdata
      newTask.subdata.push(newSubtodo) 
      return {...initialState,todos:[...initialState.todos,newTask]}

注意:将嵌套对象用作react js中的状态不是一个好
练习。

You could do something like...

// send payload as {id:1,newSubtodo: newSubtodo} 
case ADD_SUBTODO:
      const newSubtodo = action.payload.newSubtodo;
      //id is the task/todo id of which you want to add a new subdata
      const newTask = initialState.todos.find(i=>i.id==action.payload.id) 
      //id is the task/todo id of which you want to add a new subdata
      newTask.subdata.push(newSubtodo) 
      return {...initialState,todos:[...initialState.todos,newTask]}

Note: Using nested objects as state in React Js is not a good
practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文