April 2020
Intermediate to advanced
716 pages
18h 55m
English
Before the cart item details can be displayed, we need to retrieve the cart details stored in localStorage. For this purpose, we implement the getCart helper method in cart-helper.js, which retrieves and returns the cart details from localStorage, as shown in the following code.
mern-marketplace/client/cart/cart-helper.js:
getCart() { if (typeof window !== "undefined") { if (localStorage.getItem('cart')) { return JSON.parse(localStorage.getItem('cart')) } } return []}
In the CartItems component, we will retrieve the cart items using the getCart helper method and set it to the state of the initial value of cartItems, as shown in the following code.
mern-marketplace/client/cart/CartItems.js:
const [cartItems, setCartItems] ...
Read now
Unlock full access