Arpit Mishra
Arpit Mishra

Reputation: 90

What should be the correct inital value for redux state

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;
}
}

enter image description here 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

Answers (1)

Murhaf Sousli
Murhaf Sousli

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

Related Questions