Web DevelopmentIntermediate 2 to 4 hours

Persist Cart to LocalStorage

Ensure users do not lose their cart when they refresh the page.

The Scenario

The boutique store is live, but analytics show a huge drop-off. Users are adding items to their cart on their phone, getting distracted, and when they come back later or refresh the page, their cart is empty. You need to persist the cart data locally.

The Brief

Extend a basic React shopping cart to sync with the browser localStorage. When the app loads, it should check for an existing cart and initialize the state. Whenever the cart changes (item added, removed, quantity updated), it must save the new state back to localStorage.

Deliverables

  • The custom hook (e.g., `useCartStorage`) or Context provider managing the persistence logic
  • The useEffect logic handling the synchronization
  • A brief explanation of how you handle the Next.js/SSR hydration mismatch error (if applicable)

Submission Guidance

If using Next.js or SSR, remember that localStorage is not available on the server. Show how you prevent hydration errors when the server renders an empty cart but the client has items saved.

Submit Your Work

Your submission is graded against the rubric on the right. If you pass, you get a public Badge URL you can share on LinkedIn. There is no draft save, so work offline first and paste your finished response here.

This appears on your public Badge.

0/20000 charactersMarkdown supported

One per line or comma separated. Up to 5 links.

By submitting, you agree your submission text, name, and evaluation will appear on a public Badge URL.