简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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