`"use client";
import { createContext, useContext, useState } from "react";
import { toast } from "react-hot-toast";
const Context = createContext();
export const StateContext = ({ children }) => {
// Get all data from local storage
const cartItemsFromStorage = JSON.parse(window.localStorage.getItem("cartItems"));
const totalPriceFromStorage = JSON.parse(window.localStorage.getItem("totalPrice"));
const totalQuantitiesFromStorage = JSON.parse(
window.localStorage.getItem("totalQuantities")
);
const qtyFromStorage = JSON.parse(window.localStorage.getItem("qty"));
const [showCart, setShowCart] = useState(false);
// set the data from local storage as the default state and if it doesn't exist add default state
const [cartItems, setCartItems] = useState(cartItemsFromStorage || []);
const [totalPrice, setTotalPrice] = useState(totalPriceFromStorage || 0);
const [totalQuantities, setTotalQuantities] = useState(
totalQuantitiesFromStorage || 0
);
const [qty, setQty] = useState(qtyFromStorage || 1);
// set local storage data
window.localStorage.setItem("totalQuantities", totalQuantities);
window.localStorage.setItem("totalPrice", totalPrice);
window.localStorage.setItem("qty", qty);
so we going to get all products from the local storage and not from the state context anymore for instance if you want to get the cartItms array in Cart.JSX you have to write following code const cartItems = JSON.parse(window.localStorage.getItem("cartItems"));
and so on for total price and total quantities const totalPrice = JSON.parse(window.localStorage.getItem("totalPrice")); const totalQuantities = JSON.parse(window.localStorage.getItem("totalQuantities"));
And if you have any issues here is my repo
it's next.js 13 but it's not entirely done there are some bugs in the building process.
`"use client"; import { createContext, useContext, useState } from "react"; import { toast } from "react-hot-toast";
const Context = createContext();
export const StateContext = ({ children }) => { // Get all data from local storage const cartItemsFromStorage = JSON.parse(window.localStorage.getItem("cartItems")); const totalPriceFromStorage = JSON.parse(window.localStorage.getItem("totalPrice")); const totalQuantitiesFromStorage = JSON.parse( window.localStorage.getItem("totalQuantities") ); const qtyFromStorage = JSON.parse(window.localStorage.getItem("qty"));
const [showCart, setShowCart] = useState(false);
// set the data from local storage as the default state and if it doesn't exist add default state const [cartItems, setCartItems] = useState(cartItemsFromStorage || []); const [totalPrice, setTotalPrice] = useState(totalPriceFromStorage || 0); const [totalQuantities, setTotalQuantities] = useState( totalQuantitiesFromStorage || 0 ); const [qty, setQty] = useState(qtyFromStorage || 1); // set local storage data window.localStorage.setItem("totalQuantities", totalQuantities); window.localStorage.setItem("totalPrice", totalPrice); window.localStorage.setItem("qty", qty);
let foundProduct; let index;
const onAdd = (product, quantity) => { const checkProductInCart = cartItems.find( (item) => item._id === product._id );
};
const onRemove = (product) => { foundProduct = cartItems.find((item) => item._id === product._id); const newCartItems = cartItems.filter((item) => item._id !== product._id);
};
const toggleCartItemQuantity = (id, value) => { foundProduct = cartItems.find((item) => item._id === id); index = cartItems.findIndex((product) => product._id === id); const newCartItems = cartItems.filter((item) => item._id !== id);
};
const incQty = () => { setQty((prevQty) => prevQty + 1); };
const decQty = () => { setQty((prevQty) => { if (prevQty - 1 < 1) return 1;
};
return ( <Context.Provider value={{ showCart, setShowCart, incQty, decQty, onAdd, toggleCartItemQuantity, onRemove, setCartItems, setTotalPrice, setTotalQuantities, setQty }}
export const useStateContext = () => useContext(Context); `
so we going to get all products from the local storage and not from the state context anymore for instance if you want to get the cartItms array in Cart.JSX you have to write following code
const cartItems = JSON.parse(window.localStorage.getItem("cartItems"));
and so on for total price and total quantitiesconst totalPrice = JSON.parse(window.localStorage.getItem("totalPrice")); const totalQuantities = JSON.parse(window.localStorage.getItem("totalQuantities"));
And if you have any issues here is my repo it's next.js 13 but it's not entirely done there are some bugs in the building process.