Manually fetch user
nextjs sdk:
Is it possible to fetch the user inside a function? Because on component mount, it's null. Then after some time, it loads. I want to track the progress with a loading state like the other fetches in fetchData. How do I do that? It's running the useEffect twice which messes up the loading state.
3 Replies
Hey @zhefciad,
To fetch the user inside a function and track the loading state, you can utilize the
getKindeServerSession
helper function provided by Kinde in your Next.js API route. This allows you to fetch user data on the server side and then call this API endpoint from your component to manage the loading state effectively.
Here's how you can do it:
1. Create an API route in your Next.js project that returns the user data from getKindeServerSession
. This step is crucial as it allows you to fetch user data on the server side, which you can then call from your component.
2. Modify your component to fetch this API route. This way, you can manage the loading state just like you do with other fetches in fetchData
.
By fetching the user data through an API route, you encapsulate the user fetching logic on the server side, which allows you to manage the loading state in your component effectively. This approach also avoids the issue of the useEffect
hook running twice due to the user
dependency, as the user fetching is now part of the initial data fetching logic.Thanks! Exactly what I needed.
No worries