简体   繁体   English

如何在这个例子中添加“部分”/ redux

[英]How to add “parts” in this example / redux

How to add "parts" in this example The problem is that I can't add a new object如何在本例中添加“零件” 问题是我无法添加新对象

My current reducer:我目前的减速机:

case "ADD_PART": {

      return {
        ...state,
        exercise: state.exercise.map((items, id) =>
          id === action.index ? items.parts[0].repeating : items
        ),
      };
    }

state before之前的状态

在此处输入图片说明

{
    "exercise": [
        {
            "id": "60e07e588a2f1946f895475f",
            "name": "Wyciskanie sztangi",
            "image": "uploads\\2021-07-03T15-12-24.173Z-test.gif",
            "instruction": "Opierając tułów na ławce weź sztangielki",
            "parts": [
                {
                    "weight": 30,
                    "repeating": 10
                }
            ],
            "isOpen": true
        }
    ]
}

State after run reducer it changes the object to me运行减速器后的状态它将对象更改为我

减速机运行后的状态

{
    "exercise": [
        10
    ]
}

使用id迭代器作为要从items数组中选取的元素的索引。

id === action.index ? items[id].parts[0].repeating : items

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

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