How to get data from server into zustand store instantly
Unanswered
Keyhole wasp posted this in #help-forum
Keyhole waspOP
I am fetching the user in the server and returning it to layout as a header "X-USER". The data is there on first render. Now, how do I immediately put it in zustand store? All the guides I can find say to use useEffect and setUser there, but obviously this results in a horrid first render with user as null as it renders before useEffect runs. What am I supposed to do? Not use zustand and instead prop drill user data into the entire app? That would at least work on first render.
2 Replies
@Keyhole wasp I am fetching the user in the server and returning it to layout as a header "X-USER". The data is there on first render. Now, how do I immediately put it in zustand store? All the guides I can find say to use useEffect and setUser there, but obviously this results in a horrid first render with user as null as it renders before useEffect runs. What am I supposed to do? Not use zustand and instead prop drill user data into the entire app? That would at least work on first render.
you have two possible solutions:
1. Fetching on the server and display the data instantly (SSR)
2. Fetching on the server and pass it as initial value to your provider (zustand).
Btw, it's not a good practice to use headers to transfer a lot of data
1. Fetching on the server and display the data instantly (SSR)
2. Fetching on the server and pass it as initial value to your provider (zustand).
Btw, it's not a good practice to use headers to transfer a lot of data
@Keyhole wasp solved?