Next.js Discord

Discord Forum

how can I make a page load only when I have data inside useState?

Answered
subscr posted this in #help-forum
Open in Discord
I tried it that way, but it doesnt work and it seems to be bad.
Answered by James4u
useEffect(() => {
...
}, []);

if (status === "loading") return <SocketLoader />
return <SocketProvider ... />
View full answer

48 Replies

i'm using next-auth, and it takes a few seconds to get the id
@subscr
depending on the status, you can show spinner
if you don't need any spinner or skeleton UI and just need to show something when data is available
just return null
if status === "loading" return spinner?
if so, before useEffect, right?
ofc, while you are fetching you have nothing to show, right?
ye
and ofc if you want to tell the user that fetching is in progress
yes
like this?
sorry, my bad
your new code line should be after useEffect
useEffect(() => {
...
}, []);

if (status === "loading") return <SocketLoader />
return <SocketProvider ... />
Answer
hooks shouldn't be conditionally called - you may get build error
hmm, worked
thansk bro, you are my hero
:sunglasses_1::sunglasses_1:
yr welcome! mark solution if it helped you 🙂
@James4u not worked, idk, userId is undefined
in session.user.id
it creates a normal jwt (api fetch), but when decrypts, returns the id (key) as undefined
okay so let's do this
if (status === "loading") return <SocketLoader />
if (!token) return null;
return <SocketProvider ... />
@subscr Try this
hmmm
@James4u oh, I misunderstood your question
the token I have normal, it returns the jwt all right, but inside the jwt, the user id (key) is undefined.
I have no context how you encrypt/decrypt
here
rest assured that "seyfert" will be changed
I think the way you get query param is incorrect
try console.log(key) before sign and see if you really get it
ok
undefined
probably because the session took a long time to load and was already direct to useEffect.
i'll create a new topic for that