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.

SSR issues with Next.js (and persisting the data)

See original GitHub issue

Hi there,

I am loving the way that Zustand works and absolutely enjoy using it, but unfortunately I have issues with the following scenario:

  • I have a “user store” that is a Zustand store that saves the “selected user ID”.
  • I have a couple of buttons that allows me to select a specific user (but the ID only) and saves it into the store.

Then I am trying to display the selected user based on the ID that is stored in the store. Through a array.find(), where I loop through my users array. Everything works fine so far, but as soon as I want to display a property from that selected user, I get the following error when I refresh the page:

image

This error only happens whenever I refresh the page so it must be an issue with the “persist data” functionality. I have made a Codesandbox to reproduce this issue and I hope someone is able to help me out with this.

https://codesandbox.io/s/nextjs-zustand-example-z8ujs?file=/pages/index.tsx

Thanks a lot and I hope someone is able to help me out.

Edit: Apparently this issue doesn’t even come from the part that filters the users based on the ID. It also throws the error in the console from only showing the “selected user ID” directly from the store, that is persisted. Really strange issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18

github_iconTop GitHub Comments

6reactions
AnatoleLucetcommented, Feb 7, 2022

@UlisseMini as shown in the doc, this is intentional.

One way to get the behavior you want is to use an asynchronous storage. You could create your own reusable persist async middleware by promisifying your storage (as shown here) :

export const asyncPersist = (config, options) => {
  const { getStorage } = options;

  options.getStorage = () => {
    const { setItem, getItem, removeItem } = getStorage();
    
    return {
      setItem: async (...args) => setItem(...args),
      getItem: async (...args) => getItem(...args),
      removeItem: async (...args) => removeItem(...args),
    }
  };

  return persist(config, options);
};


const useStore = create(asyncPersist(
  (set, get) => ({
    ...
  }),
  {
    ...
  }
))
4reactions
AnatoleLucetcommented, Mar 15, 2021

@sandren ah, my bad. This part: “it can be done with a simple condition based on this persist option” is wrong. I thought it’d be sufficient, but apparently it isn’t. I’ve forked @gino’s csb: https://codesandbox.io/s/nextjs-zustand-example-forked-bnpfz?file=/pages/index.tsx The trick is the useHasHydrated hook. If you conditionally render elements based on your store using this hook, you won’t have the warning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bridging the Gap Between SSR and Client-State Persisted ...
While Next. js is great for SSR and SSG, there isn't an intuitive way to persist client-side data.
Read more >
nextjs ssr cause client side state persist conflict - Stack Overflow
I use zustand to manage my states. I persist the width state in user local storage then reload ...
Read more >
Managing persistent States in Nextjs with Zustand - Dev Genius
Creating and managing persistent state with Zustand in a Nextjs project. The approach and workaround to have it work smoothly.
Read more >
Going to Production - Next.js
Before taking your Next.js application to production, here are some recommendations to ensure the best user experience.
Read more >
How to Use Cookies to Persist State in NextJS - Medium
There are a number of ways to persist users in a React or Single Page Application. A lot of times, devs generally use...
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