Best practices to handle state on serverside
Answered
Dwarf Crocodile posted this in #help-forum
Dwarf CrocodileOP
Is there a recommended solution, like zustand for client side, to handle state in server components? It feels unneccesary that were back i prop drilling in 2024.
17 Replies
@Dwarf Crocodile Is there a recommended solution, like zustand for client side, to handle state in server components? It feels unneccesary that were back i prop drilling in 2024.
if you share the state on server, the state will share along with different user.
with server component, we could fetch the data we need in the component
with server component, we could fetch the data we need in the component
Dwarf CrocodileOP
Yeah i know but if i want to validate the users session in three components, i wonder if there is a good way to not have to do the same call in all of them.
Answer
from lucia doc
Dwarf CrocodileOP
If i cache a validateSessionToken in my layout and then export it to it's children. Then all children will get the same result and the session wont bleed to other user, if i'm understanding this correctly?
@Dwarf Crocodile If i cache a validateSessionToken in my layout and then export it to it's children. Then all children will get the same result and the session wont bleed to other user, if i'm understanding this correctly?
yes the result from react cache only live in a single request
Dwarf CrocodileOP
Or should i cache it where i define the method, I don't really understand how this works. Will it work the same if if i cache the original method?
Oh perfcet
seems like what i want
when the request is done, the result is clear
Dwarf CrocodileOP
Should i cache the original function or create a copy?
what do you mean?
Dwarf CrocodileOP
should i
or
const cachedValidation = cache(validateLogin);or
const validateLogin = cache(async() => {
return login()
})const cachedFetchUsers = cache(fetchUsers)both are fine, but I prefer this
so I know that function is a react cache version
Dwarf CrocodileOP
Ok, thank you!