I am trying to add products to shopping cart by storing them in local storage but, they are not getting stored. I looked into Redux-dev-tools and found out my state is not updating:
As you can see action is getting fired but my state is not updating:
Here is the source code:
cartAction.js
JavaScript
x
17
17
1
import axios from "axios"; import { CART_ADD_ITEM, CART_REMOVE_ITEM } from "../constants/cartConstants";
2
3
export const addToCart = (id, qty) => async (dispatch, getState) => { const { data } = await axios.get(`/api/product/${id}`);
4
5
dispatch({
6
type: CART_ADD_ITEM,
7
payload: {
8
productID: data.product._id,
9
name: data.product.name,
10
image: data.product.image,
11
price: data.product.price,
12
countInStock: data.product.countInStock,
13
qty,
14
}, });
15
16
localStorage.setItem("cartItems", JSON.stringify(getState().cart.cartItems)); };
17
cartReducer.js
JavaScript
1
30
30
1
import { CART_ADD_ITEM, CART_REMOVE_ITEM } from "../constants/cartConstants";
2
3
export const cartReducer = (state = { cartItems: [] }, action) => {
4
switch (action.state) {
5
case CART_ADD_ITEM:
6
const item = action.payload;
7
const existItem = state.cartItems.find(
8
(x) => x.productID === item.productID
9
);
10
11
if (existItem) {
12
return {
13
state,
14
cartItems: state.cartItems.map((x) =>
15
x.productID === existItem.productID ? item : x
16
),
17
};
18
} else {
19
return {
20
state,
21
cartItems: [state.cartItems, item],
22
};
23
}
24
// case CART_REMOVE_ITEM:
25
// return {};
26
default:
27
return state;
28
}
29
};
30
store.js
JavaScript
1
34
34
1
import { createStore, combineReducers, applyMiddleware } from "redux";
2
import thunk from "redux-thunk";
3
import { composeWithDevTools } from "redux-devtools-extension";
4
5
// reducers
6
import {
7
productDetailsReducer,
8
productListReducer,
9
} from "./reducers/productReducers";
10
import { cartReducer } from "./reducers/cartReducers";
11
12
const reducer = combineReducers({
13
productList: productListReducer,
14
productDetails: productDetailsReducer,
15
cart: cartReducer,
16
});
17
18
const cartItemsFromStorage = localStorage.getItem("cartItems")
19
? JSON.parse(localStorage.getItem("cartItems"))
20
: [];
21
22
const initialState = {
23
cart: { cartItems: cartItemsFromStorage },
24
};
25
const middleware = [thunk];
26
27
const store = createStore(
28
reducer,
29
initialState,
30
composeWithDevTools(applyMiddleware(middleware))
31
);
32
33
export default store;
34
CartScreen.js
JavaScript
1
33
33
1
import React, { useEffect } from "react";
2
import { useDispatch, useSelector } from "react-redux";
3
import { addToCart, removeFromCart } from "../../redux/actions/cartActions";
4
import { Link } from "react-router-dom";
5
import ErrorMessage from "../../components/ErrorMessage/ErrorMessage";
6
import "./CartScreen.scss";
7
8
const CartScreen = ({ match, location, history }) => {
9
const productID = match.params.id;
10
11
const qty = location.search ? Number(location.search.split("=")[1]) : 1;
12
13
const dispatch = useDispatch();
14
15
const cart = useSelector((state) => state.cart);
16
const { cartItems } = cart;
17
18
console.log(cartItems);
19
useEffect(() => {
20
if (productID) {
21
dispatch(addToCart(productID, qty));
22
}
23
}, [dispatch, productID, qty]);
24
25
return (
26
<>
27
<h1>Shopping Cart</h1>
28
</>
29
);
30
};
31
32
export default CartScreen;
33
Advertisement
Answer
You need to fix this on CartReducer.js
JavaScript
1
2
1
switch (action.state) {
2
to
JavaScript
1
2
1
switch (action.type) {
2