我正试图在物品阵列中添加一个新对象,该对象位于特定的商店中,但redux没有重新渲染。我尝试过spread和concat方法,因为不推荐使用push功能,但它不起作用。
const initialState ={
stores: []
}
//提取数据后的存储如下所示:
stores: [
{
name: "iPhone store",
id: 12345,
items: [
{id: 1233, name: "iPhone 7"}
]
}
]
我想在一个特定的商店里添加一个新项目,减速器看起来像这样:
const store_new_item = (state, action) => {
// not re-rendering
let stores = [...state.stores];
const index = stores.findIndex((a) => a._id === action.storeid);
stores[index] = {
...stores[index],
items: stores[index].items.concat({ ...action.item, _id: nanoid() }),
};
return {
...state,
loading: false,
stores: stores,
};
};
感谢您的反馈!
stores[index] = {
...stores[index],
items: [...stores[index].items, action.item],
};