Reputation: 90
I wanted to create 'add to cart' functionality using Redux, so I have defined a reducer function which accepts a state and action, where state is provided a default value.
export const defaultValue= {
products: [
{
id: "",
title: "",
description: "",
image: "",
price: 0,
amount: 0,
},
]
};
export const manageCart = (
state = defaultValue,
action: { type: string; payload: any}
) => {
switch (action.type) {
case actionConstants.ADD_TO_CARTS:
{
// ADD to card
}
break;
case actionConstants.REMOVE_FROM_CART: {
// Remove from card
}
default:
return state;
}
}
So by default I'm seeing this blank product on my cart which is messing up my logic moving further. Since it is mandatory to provide initial value to the state, what should i do differently to get rid of this issue.
Upvotes: 0
Views: 33
Reputation: 13296
So by default I'm seeing this blank product on my cart which is messing up my logic moving further.
Because you are passing that blank product in the default state!
Just pass an empty array instead
export const defaultValue= {
products: []
};
Upvotes: 1