File tree Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import React, {
6
6
JSXElementConstructor ,
7
7
ReactFragment ,
8
8
ReactPortal ,
9
+ useMemo ,
9
10
} from 'react' ;
10
11
11
12
interface ICartProviderProps {
@@ -52,7 +53,7 @@ interface ICartContext {
52
53
updateCart : ( newCart : RootObject ) => void ;
53
54
}
54
55
55
- const CartState = {
56
+ const CartState : ICartContext = {
56
57
cart : null ,
57
58
setCart : ( ) => { } ,
58
59
updateCart : ( ) => { } ,
@@ -70,7 +71,6 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
70
71
// Check if we are client-side before we access the localStorage
71
72
if ( typeof window !== 'undefined' ) {
72
73
const localCartData = localStorage . getItem ( 'woocommerce-cart' ) ;
73
-
74
74
if ( localCartData ) {
75
75
const cartData : RootObject = JSON . parse ( localCartData ) ;
76
76
setCart ( cartData ) ;
@@ -85,8 +85,12 @@ export const CartProvider = ({ children }: ICartProviderProps) => {
85
85
}
86
86
} ;
87
87
88
+ const contextValue = useMemo ( ( ) => {
89
+ return { cart, setCart, updateCart } ;
90
+ } , [ cart ] ) ;
91
+
88
92
return (
89
- < CartContext . Provider value = { { cart , setCart , updateCart } } >
93
+ < CartContext . Provider value = { contextValue } >
90
94
{ children }
91
95
</ CartContext . Provider >
92
96
) ;
You can’t perform that action at this time.
0 commit comments