【问题标题】:Change Key After Action in Redux在 Redux 中操作后更改键
【发布时间】:2021-10-07 01:35:17
【问题描述】:

添加数据后,来自我的后端的响应带有id。 原始列表包含book_id 而不是id。执行添加功能后如何将id更改为book_id

原始列表值

[{
    "f_name": "rwf",
    "m_name": null,
    "l_name": "f",
    "book_id": 12
}]

添加后的新响应

{
    "f_name": "WWW",
    "m_name": null,
    "l_name": "HHH",
    "id": 13
}

添加减速器代码

return {
    ...state,
    books: {
      ...state.books,
      excluded_contacts: [...state.books.excluded_contacts, action.payload],
    },
    isLoading: true,
};

【问题讨论】:

    标签: reactjs redux ecmascript-6


    【解决方案1】:

    您可以这样更新,将id 更改为book_id

    const { id, ...props } = action.payload;
    return {
      ...state,
      books: {
        ...state.books,
        excluded_contacts: [...state.books.excluded_contacts, { ...props, book_id: id }],
      },
      isLoading: true,
    };
    

    【讨论】:

    • 这个id 导致页面加载时未定义。 const { id, ...props } = action.payload;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2019-12-17
    • 2021-11-26
    • 2020-10-18
    • 2016-08-21
    • 2018-08-10
    相关资源
    最近更新 更多