I have an account page, which the user ends up after signing up. As soon as they hit this page, I would like to create a profile row with a 1-1 mapping to a (Supabase) users table.
If this would be a client side page, I would do something like this:
const ProfilePage: FC<{ userId: string }> = ({ userId }) => {
const [profile, setProfile] = useState<Profile | null>(null);
useEffect(() => {
getProfile().then(profile => {
if (profile) {
setProfile(profile)
} else {
createProfile({ user_id: userId, name: email.split("@")[0], avatar: null })
.then(setProfile)
}
})
}, [])
return profile ? (
<div>Welcome, {profile.name}</div>
) : (
<div>Loading your profile / setting up your profile ...</div>
)
}
Now I want to do this in a server component. If I understand correctly, useEffect()
is a client-side hook. How do I check if something exists, run something, and make sure to re-render the page as soon as the profile has been created?
2
Answers
the code inside the
useEffect
is related to the supabase user. you can usecreateServerComponentClient
on the server.I showed the getting or creating profile in the same function. But actually using
server actions
would be much better toinsert
the profile. Example from hereIn this case I think just a boolean reference indicating the first render is simplest.
Demo: https://stackblitz.com/edit/stackblitz-starters-d3z6hx?file=pages%2Fprofile.tsx