refactor: replace React Context with Zustand for state management#32
Open
devin-ai-integration[bot] wants to merge 1 commit into
Open
refactor: replace React Context with Zustand for state management#32devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
- Replace context/StateContext.js with Zustand store (zustand v4) - Fix typo: toggleCartItemQuanitity -> toggleCartItemQuantity - Fix mutation bug: no longer mutates product.quantity prop directly - Fix map bug: .map() now returns cartProduct for non-matching items - Fix desync bug: totalPrice and totalQuantities are derived from cartItems - Add cart persistence via zustand/middleware persist with localStorage - Add clearCart action for success page - Update Cart.jsx to use fixed toggleCartItemQuantity name - Update success.js to use clearCart instead of manual state reset - Add ErrorBoundary component wrapping the app in _app.js - Keep backward-compatible StateContext wrapper and useStateContext hook Co-Authored-By: Arjun Mishra <arjunsaxmishra@gmail.com>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the buggy React Context-based state management in
context/StateContext.jswith a Zustand store (v4.5.7, compatible with React 17). Fixes four known bugs, adds cart persistence via localStorage, and wraps the app in an error boundary.Bug fixes:
toggleCartItemQuanitity→toggleCartItemQuantity(in store, Cart.jsx destructuring, and onClick handlers)onAddno longer mutates theproductprop directly (product.quantity = quantity); instead creates a new object via spread.map()inonAddnow returnscartProductfor non-matching items (previously returnedundefined)totalPriceandtotalQuantitiesare now derived fromcartItemson every read instead of tracked as separate stateNew features:
zustand/middlewarepersist (keyed ascart-storage, onlycartItemsare persisted)clearCartaction replaces the manuallocalStorage.clear()+ three separate setter calls on the success pageErrorBoundaryclass component wraps the entire app in_app.jsBackward compatibility:
StateContextcomponent is kept as a pass-through wrapper (<>{children}</>) so_app.jsdoesn't need structural changes beyond adding the ErrorBoundary.Review & Testing Checklist for Human
npm run buildpasses but no runtime UI testing was performed. Add items, increment/decrement quantities, remove items, and confirm totals update correctly.cart-storagekey).localStorage.clear()behavior change insuccess.js— the old code cleared ALL localStorage; the new code only resets the Zustand store (which updates thecart-storagekey). Confirm this is acceptable and no other localStorage keys depended on being cleared here.className="error-boundary"but no CSS was added (per ticket constraint of no styling changes). Verify the unstyled fallback is acceptable or if a follow-up is needed.Suggested test plan: Run
npm run dev, add multiple products to the cart, adjust quantities up/down, remove an item, verify the subtotal is correct, refresh the page to confirm persistence, then click "Pay with Stripe" or navigate to/successto verify the cart clears.Notes
useSyncExternalStorefrom React 18, and this project runs React 17.useStateContext()hook subscribes to the full store (no granular selectors), matching the previous Context re-render behavior. Performance optimization with selectors can be a follow-up.showCartandqtyare intentionally not persisted — cart drawer should be closed and quantity selector should reset on page refresh.Link to Devin session: https://app.devin.ai/sessions/d85ed244776d42ac81f8e2e663f26032
Requested by: @Colhodm