【发布时间】:2018-11-28 17:39:13
【问题描述】:
我在 redux 中有这个状态:
draft: {
temporary: { clientId: null, shoppingCart: [] },
},
在我的购物车中,我使用 react-addons-update 推送一些对象:
const newState = { ...state };
let cart = newState.draft.temporary.shoppingCart;
cart = update(cart, { $push: [{ id: 1, quantity: 3 }] });
newState.draft.temporary.shoppingCart = cart;
return newState;
而我在应用中的 newState 是:
draft: {
temporary: { clientId: null, shoppingCart: [
{id: 1, qnt: 3},
]},
},
我将我的 products 减速器配置为:
products: persistReducer(products: {
key: 'products',
storage,
whitelist: ['draft'],
}, products),
但是一旦重新加载应用程序,我的商店就不会持久化。
更新:
有效的示例解决方案:
newState.draft = { ...newState.draft, temporary: { ...newState.draft.temporary, shoppingCart: [ ...newState.draft.temporary.shoppingCart, { id: 1, qnt: 3 } ]}};
没有 react-addons-update 的解决方案不起作用:
newState.draft.temporary.shoppingCart = [ ...newState.draft.temporary.shoppingCart, { id: payload.id, quantity: 1 } ];
有“更清洁的解决方案”吗??
【问题讨论】:
标签: react-native redux redux-persist