In this article, we'll discuss the common error that arises when using Zustand's persist middleware with Next.js. You might have received errors li...
For further actions, you may consider blocking this person and/or reporting abuse
How would you handle this case?
Because now the
useStore
returns undefined on first render.Also, even if we use four individual
useStore
s for every variable above, all of the values are undefined. Which means we have to add a ton of checks in JSX, right?Yes, I believe it would be beneficial to develop a Higher-Order Component (HOC) for performing checks or invoke the
useStore
function in the parent component with a single check and pass data as props in your case. We are redefining the store values after hydration, so we will have anundefined
state initially. Let me know if you found any better way to tackle this problem.Hi
Did you find a solution to the same? I am also getting undefined on the first render. Adding checks on all the pages will be too tedious
@keshav263 We added checks in our app. Fortunately, we had only two places where we had to modify the JSX and add the if statements. We didn't have to modify the entire app because the other components are rendered "later" in the rendering lifecyle due to us having loading screens. So by the time the loading screens disappear, the store has the values in it
Question: how did you add your checks? I'm struggling to do it once functions are involved, too
@nobilelucifero We ended up creating this hook and used it two-three times in the app:
In the code, it can be used like this:
The check is just that if statement in the JSX
I am not recommending to used this too many times because it makes the JSX full of ternary operators like that. The reason we use it just three times is because we have a loading screen which waits for the backend data to load. In that time, the currency is already loaded and we don't need to do that ternary checks. We only have three places that don't have a loading section and that's why we used this custom hook three times.
ahhh I see! It was simpler than what I was trying to (check to hydration and whether it was a variable or function). Amazing, thank you so so so much. I hope in a more proper solution soon too.
Awesome, this is the kind of solution I was looking for, linked it in this issue too: github.com/pmndrs/zustand/issues/938
Thanks!
I'm glad it helped!
or maybe you can create a check for hydration render a loading.. while your ui do not get hydrated
Yes. I think that should work as well.