0
0
NextJSframework~20 mins

Testing server components 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 rendered output of this Next.js server component?
Consider this server component in Next.js 14+ that fetches data and renders it. What will be the output when rendered?
NextJS
import React from 'react';

async function getData() {
  return { message: 'Hello from server!' };
}

export default async function Greeting() {
  const data = await getData();
  return <h1>{data.message}</h1>;
}
AError: Cannot use await in server component
B<h1>undefined</h1>
C<h1>Hello from client!</h1>
D<h1>Hello from server!</h1>
Attempts:
2 left
💡 Hint
Server components can use async/await to fetch data before rendering.
lifecycle
intermediate
1:30remaining
When does a Next.js server component run its code?
In Next.js 14+, when does the code inside a server component execute?
AOnly on the server during the initial page load and on server-side navigation
BOnly on the client after hydration
COn both client and server simultaneously
DOnly when a user clicks a button inside the component
Attempts:
2 left
💡 Hint
Server components run where the server renders the page.
🔧 Debug
advanced
2:30remaining
Why does this server component cause a hydration mismatch?
Examine this server component code. Why does it cause a hydration mismatch error in Next.js?
NextJS
import React from 'react';

export default function Time() {
  const now = new Date().toISOString();
  return <p>{now}</p>;
}
ABecause the component uses Date inside a server component, causing different output on server and client
BBecause server components cannot return JSX elements
CBecause the component uses React hooks incorrectly
DBecause the component is missing async keyword
Attempts:
2 left
💡 Hint
Server components render once on the server, but client hydration expects stable output.
📝 Syntax
advanced
3:00remaining
Which option correctly defines a server component with a server action in Next.js 14+?
Select the correct syntax for a server component that includes a server action to handle a form submission.
A
export default function Form() {
  function submit(data) {
    console.log(data);
  }
  return &lt;form onSubmit={submit}&gt;&lt;button type="submit"&gt;Send&lt;/button&gt;&lt;/form&gt;;
}
B
export default async function Form() {
  async function submit(data) {
    'use server';
    console.log(data);
  }
  return &lt;form action={submit}&gt;&lt;button type="submit"&gt;Send&lt;/button&gt;&lt;/form&gt;;
}
C
export default async function Form() {
  function submit(data) {
    'use server';
    console.log(data);
  }
  return &lt;form action={submit}&gt;&lt;button type="submit"&gt;Send&lt;/button&gt;&lt;/form&gt;;
}
D
export default function Form() {
  async function submit(data) {
    console.log(data);
  }
  return &lt;form action={submit}&gt;&lt;button type="submit"&gt;Send&lt;/button&gt;&lt;/form&gt;;
}
Attempts:
2 left
💡 Hint
Server actions require the 'use server' directive inside the function and must be async.
state_output
expert
3:00remaining
What is the value of count after rendering this mixed server/client component setup?
Given this Next.js 14+ setup with a server component rendering a client component, what is the final count value displayed after clicking the button twice?
NextJS
import React, { useState } from 'react';

// Client component
'use client';
export function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

// Server component
export default function Page() {
  return <Counter />;
}
ACount: 1
BCount: 0
CCount: 2
DCount: undefined
Attempts:
2 left
💡 Hint
Client components manage state and update on user interaction.