Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix: Move useOptimistic to provider level for consistent cart state#1469

Open
francomay3 wants to merge 1 commit intovercel:main from
francomay3:main
Open

Fix: Move useOptimistic to provider level for consistent cart state #1469
francomay3 wants to merge 1 commit intovercel:main from
francomay3:main

Conversation

@francomay3
Copy link

@francomay3 francomay3 commented Jul 1, 2025
edited
Loading

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
Contributor

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

Comments

AltStyle によって変換されたページ (->オリジナル) /