I am using Redux to manage the cart state but on restarting the app, the cart state becomes empty. To resolve, I thought of using AsyncStorage to store the data and get it in componentDidMount but it shows null instead of the cart data.
REDUX ACTION CODE
case 'ADD_TO_CART':
{
const updatedCart = [...state.cart];
const item = updatedCart.find(x=>x.key===action.payload.key);
if(item)
{
item.count++;
}
else{
updatedCart.push(action.payload);
}
AsyncStorage.setItem('cart',{...state,cart: updatedCart,total: state.total + 1})
return {
...state,
cart: updatedCart,
total: state.total + 1,
}
}
FETCHING DATA
async componentDidMount() {
const item = await AsyncStorage.getItem('cart'))
console.log(item)
}
I have one more action to delete the cart item in which the same setItem method is used to set the cart state.