0
0
NextJSframework~20 mins

Server component execution model in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server Component Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js Server Component?
Consider this Next.js Server Component code. What will it render on the page?
NextJS
export default function Greeting() {
  const name = 'Alice';
  return <h1>Hello, {name}!</h1>;
}
AError: name is undefined
B<h1>Hello, Alice!</h1>
C<h1>Hello, {name}!</h1>
D<h1>Hello!</h1>
Attempts:
2 left
💡 Hint
Server components run on the server and can use variables directly in JSX.
lifecycle
intermediate
2:00remaining
When does a Next.js Server Component run?
At what point does a Next.js Server Component execute its code?
AOn the server during the initial page request before sending HTML
BIn the browser after the page loads
COnly when a user clicks a button
DAfter the client-side JavaScript finishes loading
Attempts:
2 left
💡 Hint
Think about where server components run compared to client components.
state_output
advanced
2:00remaining
What happens if you use useState in a Next.js Server Component?
Examine this code snippet. What will happen when this Server Component runs? export default function Counter() { const [count, setCount] = useState(0); return

{count}

; }
NextJS
export default function Counter() {
  const [count, setCount] = useState(0);
  return <p>{count}</p>;
}
A<p>undefined</p> rendered
B<p>0</p> rendered successfully
CRuntime error: setCount is undefined
DSyntaxError: useState cannot be used in Server Components
Attempts:
2 left
💡 Hint
Remember which hooks are allowed in Server Components.
📝 Syntax
advanced
2:00remaining
Which code correctly fetches data in a Next.js Server Component?
Choose the option that correctly fetches data inside a Server Component and renders it.
A
export default async function Data() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return &lt;pre&gt;{JSON.stringify(data)}&lt;/pre&gt;;
}
B
export default function Data() {
  fetch('https://api.example.com/data').then(res =&gt; res.json()).then(data =&gt; {
    return &lt;pre&gt;{JSON.stringify(data)}&lt;/pre&gt;;
  });
}
C
export default function Data() {
  const data = fetch('https://api.example.com/data').then(res =&gt; res.json());
  return &lt;pre&gt;{JSON.stringify(data)}&lt;/pre&gt;;
}
D
export default async function Data() {
  const data = fetch('https://api.example.com/data').json();
  return &lt;pre&gt;{JSON.stringify(data)}&lt;/pre&gt;;
}
Attempts:
2 left
💡 Hint
Server Components can be async and await fetch calls.
🔧 Debug
expert
2:00remaining
Why does this Server Component cause a hydration mismatch error?
This Server Component renders a random number: export default function Random() { const num = Math.random(); return

{num}

; } Why might this cause a hydration mismatch error in Next.js?
NextJS
export default function Random() {
  const num = Math.random();
  return <p>{num}</p>;
}
ABecause Math.random() is not allowed in Server Components
BBecause the component must be async to use Math.random()
CBecause the server and client render different random numbers causing mismatch
DBecause the component is missing a key prop
Attempts:
2 left
💡 Hint
Think about how server-rendered HTML and client hydration must match exactly.