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, {
66 JSXElementConstructor ,
77 ReactFragment ,
88 ReactPortal ,
9+ useMemo ,
910} from 'react' ;
1011
1112interface 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 ) ;
You can’t perform that action at this time.
0 commit comments