score:0

in productreducer file you should push the item into state.cartitems

if (!producttoadd) {
        producttoadd = products.find(obj => obj.id === action.id);
        count = 1;
     add row-> state.cartitems.push(producttoadd);
}

in this approach, if you don't enter a specified quantity of the product, it will be automatically placed to the cart as one item.

score:1

it looks like the way you map the action to props does not match the actual usage of the function.

you map it like this:

addtocart: (id)=>{dispatch(addtocart(id))}

the action just passes its parameter as payload meaning payload is equal to id:

export const addtocart= payload =>({
  type: add_to_cart,
  payload
});

but you expect the payload to be an object like this:

const { id } = action.payload;

you need to make them match. either pass the action an object:

addtocart: (id)=>{dispatch(addtocart({id}))}

or use the payload as the id:

const id = action.payload;

the result of the code as is, is that id is undefined, meaning no producttoadd.


Related Query

More Query from same tag