how can I make a page load only when I have data inside useState?
Answered
subscr posted this in #help-forum
Answered by James4u
useEffect(() => {
...
}, []);
if (status === "loading") return <SocketLoader />
return <SocketProvider ... />48 Replies
subscrOP
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
just return null
if status === "loading" return spinner?
if so, before useEffect, right?
ofc, while you are fetching you have nothing to show, right?
subscrOP
ye
and ofc if you want to tell the user that fetching is in progress
subscrOP
yes
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
yr welcome! mark solution if it helped you 🙂
subscrOP
@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
subscrOP
hmmm
@subscr it creates a normal jwt (api fetch), but when decrypts, returns the id (key) as undefined
oh, I misunderstood your question
@James4u tsx
if (status === "loading") return <SocketLoader />
if (!token) return null;
return <SocketProvider ... />
but this is more appropriate one
@James4u oh, I misunderstood your question
subscrOP
the token I have normal, it returns the jwt all right, but inside the jwt, the user id (key) is undefined.
@subscr the token I have normal, it returns the jwt all right, but inside the jwt, the user id (key) is undefined.
that's a different problem from the original question
I have no context how you encrypt/decrypt
I think the way you get query param is incorrect
try
console.log(key) before sign and see if you really get itok
undefined
probably because the session took a long time to load and was already direct to useEffect.
subscrOP
i'll create a new topic for that
