我现在的状态是:
draft: {
temporary: { clientId: null, shoppingCart: [] },
},在我的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: 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 } ]}};没有反应的解决方案-加载项-不起作用的更新:
newState.draft.temporary.shoppingCart = [ ...newState.draft.temporary.shoppingCart, { id: payload.id, quantity: 1 } ];有一个“更清洁的解决方案”?
发布于 2018-11-29 10:51:39
我使用的最后一种解决方案是改变减速器:
const newDraft = { ...state.draft };
let cart = state.draft.temporary.shoppingCart;
const index = _.findIndex(cart, { id: payload.id });
if (index === -1) cart = update(cart, { $push: [{ id: payload.id, quantity: 1 }] });
else cart = update(cart, { [index]: { quantity: { $set: cart[index].quantity + 1 } } });
newDraft.temporary.shoppingCart = cart;
return { ...state, draft: { ...newDraft } };发布于 2018-11-28 20:59:14
下面是一个可以适用于您的情况的示例:
import { persistCombineReducers, persistReducer } from 'redux-persist';
const draftPersistConfig = { key: 'draft', storage: AsyncStorage };
const reducers = { products: persistReducer(draftPersistConfig, products), // ...other reducers if you have any }
const persistConfig = { key: 'root', storage: AsyncStorage, debug: true, whitelist: ['draft'] };
persistCombineReducers(persistConfig, reducers);应该返回您可以用来创建存储的rootReducer
https://stackoverflow.com/questions/53525162
复制相似问题