Consider this Next.js client component that tries to read a session value using useSession from next-auth/react. What will it render if the user is not logged in?
import { useSession } from 'next-auth/react'; export default function UserStatus() { const { data: session } = useSession(); if (!session) { return <p>Loading session...</p>; } return <p>Welcome, {session.user.name}!</p>; }
Think about what useSession returns before the session is loaded.
The useSession hook returns data as null initially if the user is not logged in or the session is loading. The component checks if session is falsy and shows "Loading session...". It does not immediately show a welcome message.
userName after this code runs?This Next.js client component uses useSession and useEffect to set a local state userName. What will userName be after the component mounts if the user is logged in with name "Alice"?
import { useSession } from 'next-auth/react'; import { useState, useEffect } from 'react'; export default function Profile() { const { data: session } = useSession(); const [userName, setUserName] = useState(''); useEffect(() => { if (session?.user?.name) { setUserName(session.user.name); } }, [session]); return <p>{userName}</p>; }
Remember when useEffect runs and how state updates work.
When the session data is available and contains the user name "Alice", the useEffect runs and sets userName to "Alice". Initially, it is empty, but after mount and session load, it updates.
Choose the code snippet that correctly uses useSession to access the user's email in a client component.
Check the exact structure returned by useSession.
The useSession hook returns an object with a data property containing the session. Option A correctly destructures data as session. Option A treats the whole return as session, which is incorrect. Option A destructures a non-existent session property. Option A destructures data but uses it directly without renaming, which is valid but option A matches the question's pattern.
This component tries to access session data but throws an error. What is the cause?
import { useSession } from 'next-auth/react'; export default function Dashboard() { const { data: session } = useSession(); return <p>{session.user.email}</p>; }
Think about the initial value of session when the component first renders.
The useSession hook returns data as null initially while loading or if no session exists. Accessing session.user.email without checking if session is null causes a TypeError.
Choose the correct statement about accessing session data on the client side in Next.js using next-auth.
Consider how useSession works and the nature of client-side data fetching.
The useSession hook fetches session data asynchronously on the client. Initially, the session data may be null while loading, so components must handle this state. Options A, B, and C are incorrect because session data is not instantly available, client components rely on hooks for session, and client components can access session data with useSession.