I have a store in redux. There is 2 arrays, womanProducts and cartProducts
JavaScript
x
11
11
1
const defaultState = {
2
womanProducts:
3
[
4
{name: "Mini Skirt", img: "https://static.pullandbear.net/2/photos//2022/I/0/1/p/8399/316/800/8399316800_4_1_8.jpg?t=1656932863233&imwidth=563", price: 35, inCart: 1, id: 1},
5
{name: "Basic Jeans", img: "https://static.pullandbear.net/2/photos//2022/I/0/1/p/8685/326/400/8685326400_4_1_8.jpg?t=1657798996961&imwidth=563", price: 39, inCart: 1, id: 2},
6
{name: "Fit Dress", img: "https://static.pullandbear.net/2/photos//2022/V/0/1/p/4390/422/611/4390422611_4_1_8.jpg?t=1643722977148&imwidth=563", price: 45, inCart: 1, id: 3},
7
{name: "Basic Sweatshirt", img: "https://static.pullandbear.net/2/photos//2021/I/0/1/p/8393/363/485/8393363485_4_1_8.jpg?t=1634212398331&imwidth=563", price: 29, inCart: 1, id: 4}
8
],
9
cartProducts: [],
10
}
11
Then I dispatch cartProducts and add women products to shopping cart (Case “ADD_PRODUCTS” to add products in cart and “REMOVE_FROM_CART” to remove items from cart) it’s working well, but “INCREMENT_PRODUCT” not working
JavaScript
1
16
16
1
const reducer = (state = defaultState, action) => {
2
switch(action.type) {
3
case "ADD_PRODUCTS":
4
return {state, cartProducts: [state.cartProducts, action.payload]}
5
case "REMOVE_FROM_CART":
6
return {state, cartProducts: state.cartProducts.filter((event) => event.id !== action.payload)}
7
8
case "INCREMENT_PRODUCT":
9
return {
10
state,
11
cartProducts: state.cartProducts.map((item) => item.inCart + action.payload)}
12
13
default:
14
return state
15
}
16
But now I need to increment and decrement prodcuts in cart by clicking button
Cart Component:
JavaScript
1
25
25
1
return(
2
<div className="cart">
3
{cartProducts.length > 0 ? cartProducts.map(item =>
4
<div className="cartItem" key={item.id}>
5
<img src={item.img} />
6
<div className="cartItemInfo">
7
<div className="itemInfo">
8
<h3>{item.name}</h3>
9
<h3>{item.price}$</h3>
10
</div>
11
<div className="quantity">
12
<button onClick={() => decrementProduct()}>-</button>
13
<p>{item.inCart}</p>
14
<button onClick={() => incrementProduct()}>+</button>
15
</div>
16
<div className="itemDelete">
17
<button onClick={() => removeItem(item.id)}><img src={require('../images/icons/trashCanIcon.png')} /></button>
18
</div>
19
</div>
20
</div>
21
): <h1>Cart is empty</h1>}
22
{cartProducts.length > 0 ? <h1 className="totalPrice">Total Price: {totalPrice}$</h1> : <></>}
23
</div>
24
)
25
And function to increment product
JavaScript
1
4
1
function incrementProduct() {
2
dispatch({type: "INCREMENT_PRODUCT", payload: 1})
3
}
4
Advertisement
Answer
First you should change your payload. It needs item id to determine which item you are incrementing
JavaScript
1
2
1
dispatch({type: "INCREMENT_PRODUCT", payload: {id: 1, increment: 1}})
2
Now, You can map the cart products; if item id is same with cart item id then increment or just return the item
JavaScript
1
12
12
1
case "INCREMENT_PRODUCT":
2
return {
3
state,
4
cartProducts: state.cartProducts.map((item) => {
5
if(item.id === action.payload.id) {
6
item.inCart += action.payload.increment;
7
}
8
return item;
9
})
10
}
11
12