question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js cart storage

See original GitHub issue

Hi!

I’m using the library with Next.js.

When trying to using the library in pages which rendered server-side, the content of the server doesn’t match the client. It seems this could be related to the cart being stored using localStorage and this is not available on the server, only in the client.

One thought it to use the storage prop, have you ran into this issue before ?

I tried using the storage prop and the following snipped fails with the following error. Could you provide an example of how the prop should be used and its initial value

function MyApp({ Component, pageProps }) {
  const [cart, setCart] = useState();
  return (
    <div>
      <CartProvider storage={() => [cart, setCart]}>
        <Layout>
          <Component {...pageProps} />;
        </Layout>
      </CartProvider>
    </div>
  );
}

image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mattdjenkinsoncommented, Feb 24, 2022

Hey, has there been a solution for this yet in NextJS? I’m getting a server and client mismatch on page reload.

1reaction
joshuabakercommented, Apr 1, 2022

@notrab That approach is working for us.

import { Fragment, useEffect, useState } from "react";
import { CartProvider as BaseCartProvider } from "react-use-cart";

export default function CartProvider({ children, ...props }) {
  const [mounted, setMounted] = useState(false);

  useEffect(() => setMounted(true));

  if (mounted) {
    return <BaseCartProvider {...props}>{children}</BaseCartProvider>;
  }

  return <Fragment>{children}</Fragment>;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Building a Next.js shopping cart app - LogRocket Blog
Learn how to build a Next.js application by building a shopping cart web app for a fictional game store with product category pages....
Read more >
next.js - NextJS localStorage and Context of Shopping Cart
Your localstorage has been writen when you reload the page. Try the following way to prevent set item in localstorage when init.
Read more >
NextJS simple shopping cart - DEV Community ‍ ‍
Setup the redux to make the magic in the client side. Initial the store component in redux. // ./store/index.js import { createStore ...
Read more >
What would you use to save cart items? : r/nextjs - Reddit
I'm making an ecommerce store. What would you use to store the cart items so they persist on reload and/or come back to...
Read more >
Create a Shopping Cart Page to Manage Products to ...
Create an eCommerce Store with Next.js and Stripe Checkout ... Instructor: [0:00] When we try to add them to cart, we can see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found