Skip to content

Commit 3c20893

Browse files
committed
Update CartProvider.tsx
1 parent bf093fe commit 3c20893

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/stores/CartProvider.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React, {
66
JSXElementConstructor,
77
ReactFragment,
88
ReactPortal,
9+
useMemo,
910
} from 'react';
1011

1112
interface ICartProviderProps {
@@ -52,7 +53,7 @@ interface ICartContext {
5253
updateCart: (newCart: RootObject) => void;
5354
}
5455

55-
const CartState = {
56+
const CartState: ICartContext = {
5657
cart: null,
5758
setCart: () => {},
5859
updateCart: () => {},
@@ -70,7 +71,6 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
7071
// Check if we are client-side before we access the localStorage
7172
if (typeof window !== 'undefined') {
7273
const localCartData = localStorage.getItem('woocommerce-cart');
73-
7474
if (localCartData) {
7575
const cartData: RootObject = JSON.parse(localCartData);
7676
setCart(cartData);
@@ -85,8 +85,12 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
8585
}
8686
};
8787

88+
const contextValue = useMemo(() => {
89+
return { cart, setCart, updateCart };
90+
}, [cart]);
91+
8892
return (
89-
<CartContext.Provider value={{ cart, setCart, updateCart }}>
93+
<CartContext.Provider value={contextValue}>
9094
{children}
9195
</CartContext.Provider>
9296
);

0 commit comments

Comments
 (0)