Next.js Discord

Discord Forum

What do you recommend me do after login?

Unanswered
tioelvis posted this in #help-forum
Open in Discord
I'm creating an app that allows you sign in. After login if everything is correct the API return the token and the userId, The app redirect the user to /profile and save the token and the userId in a cookie,
I created a function to get the user data through userId. In every component that I use information about the user I call this function, so the fetch is repeated many times. "Is there someway that I can store the user data and then get it making just one call?" I plan to use recoil, but if I use it my app becomes in "use client", right?

26 Replies

is the better way do that?
@tioelvis is the better way do that?
well what I know so far, no
What do you recommend me??
Create an context?
@tioelvis What do you recommend me??
are you rendering same ui on each page using user info?
Ehm.... what?
At example in the header
I call the function for get the name
In the settings page I also call the function for get the data
@tioelvis In the settings page I also call the function for get the data
you can use server side rendering for every page and make the ui which uses user details a client component
You mean separate each component that do the fetch?
@tioelvis You mean separate each component that do the fetch?
no, create a context and fetch user info once
hydrate it with that info
and which component uses that info make it a separate client comp
I get it
I'm going to use that idea
Thanks 😄
@tioelvis Thanks 😄
np :)
Do you know if there is any video about that? I tried to read the documentation but I did not understand it
@tioelvis Do you know if there is any video about that? I tried to read the documentation but I did not understand it
no i don't but basically, if you wrap a function inside [React.cache](https://nextjs.org/docs/app/building-your-application/caching#react-cache-function)
const getUser = cache(async () => {
  ...
});

then during server-side rendering even if you call getUser 1000 times, the logic in ... is only executed once

[fetch automatically "has" this React.cache in many cases](https://nextjs.org/docs/app/api-reference/functions/fetch) so if you use fetch to get user data you don't need to wrap it in cache
OOOOOOOH I get it, the data stored in the cache, so when I call the function this returns me what is in cache
yeah basically that
tbf nextjs caching is super complex now, i don't fully understand it myself, but about memoization then it's basically that
I get it, thank you so much