Skip to content

Conversation

francomay3
Copy link

@francomay3 francomay3 commented Jul 1, 2025

Fix: Move useOptimistic to provider level for consistent cart state

Background:
Discovered a bug where there was noticeable lag between adding products to cart and seeing the cart update. Initially confusing because useOptimistic was being used throughout the codebase, but the issue was that each useCart() hook call was creating its own optimistic cart instance.

Problem:
Cart updates had lag because useOptimistic was being created in each useCart() hook call, resulting in multiple optimistic cart instances. Components using the cart outside of the hook were seeing the actual cart (with server delay) while hook consumers saw the optimistic cart, causing inconsistent UI updates.

Solution:
Moved useOptimistic from the useCart hook to the CartProvider component, creating a single shared optimistic cart instance that all components can access through the context.

Result:
All cart operations now update instantly across the entire application, eliminating the lag between adding items and seeing them in the cart UI.

Files changed:
components/cart/cart-context.tsx

- Move useOptimistic from useCart hook to CartProvider
- Create single shared optimistic cart instance
- Eliminate lag between cart operations and UI updates
- Ensure all components see consistent cart state
Copy link

vercel bot commented Jul 1, 2025

Someone is attempting to deploy a commit to the Vercel Solutions Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant