Skip to content

Commit 553ad3c

Browse files
committed
refactor: streamline server-side rendering guard in CartProvider
- Replaced local isClient state with a global utility function to determine client-side rendering. - Updated comments for clarity on the purpose of the server-side rendering guard in relation to ShoppingCart's storage initialization.
1 parent 511ff0f commit 553ad3c

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

use-shopping-cart/react/CartProvider.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as React from 'react'
44
import { ShoppingCart } from '../core/ShoppingCart'
55
import type { CartConfig } from '../core/types'
6+
import { isClient } from '../utilities/SSR'
67

78
const CartContext = React.createContext<ShoppingCart | null>(null)
89

@@ -22,13 +23,7 @@ export function CartProvider({
2223
cartRef.current = new ShoppingCart(config)
2324
}
2425

25-
// Server-side rendering guard
26-
const [isClient, setIsClient] = React.useState(false)
27-
28-
React.useEffect(() => {
29-
setIsClient(true)
30-
}, [])
31-
26+
// Server-side rendering guard: use global isClient to match ShoppingCart's storage initialization
3227
if (config.shouldPersist !== false && !isClient) {
3328
return <>{loading}</>
3429
}

0 commit comments

Comments
 (0)