0
0
NextJSframework~20 mins

What can run in 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 output does this Server Component produce?
Consider this Next.js Server Component code. What will it render on the page?
NextJS
export default async function Greeting() {
  const name = await Promise.resolve('Alice');
  return <h1>Hello, {name}!</h1>;
}
A<h1>Hello, {name}!</h1>
B<h1>Hello, Alice!</h1>
CError: Cannot use await in Server Component
D<h1>Hello, undefined!</h1>
Attempts:
2 left
💡 Hint
Server Components can use async/await to fetch data before rendering.
📝 Syntax
intermediate
2:00remaining
Which code snippet is valid inside a Next.js Server Component?
Select the code snippet that is valid and will not cause a syntax or runtime error in a Server Component.
Aconst data = useState('hello'); return <p>{data}</p>;
Bconst data = fetch('/api/data').then(res => res.json()); return <p>{data}</p>;
Cconst fs = require('fs'); const data = fs.readFileSync('/file.txt', 'utf8'); return <p>{data}</p>;
Dimport fs from 'fs'; const data = await fs.promises.readFile('/file.txt', 'utf8'); return <p>{data}</p>;
Attempts:
2 left
💡 Hint
Server Components support async/await and Node.js APIs but do not support hooks like useState.
🔧 Debug
advanced
2:00remaining
Why does this Server Component cause a runtime error?
Analyze the code below. Why will this Server Component cause an error when rendering?
NextJS
import { useState } from 'react';

export default function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
AuseState is not allowed in Server Components and causes a runtime error.
BThe count variable is not initialized causing a ReferenceError.
CThe component is missing async keyword for Server Components.
DThe onClick handler is missing parentheses causing a syntax error.
Attempts:
2 left
💡 Hint
Server Components cannot use React hooks that require client-side interactivity.
🧠 Conceptual
advanced
2:00remaining
Which of these can run inside a Next.js Server Component?
Select the option that correctly describes what can run inside a Server Component.
AEvent handlers like onClick and onChange for user interactions.
BDirect DOM manipulation and browser-only APIs like window or document.
CAsync data fetching, Node.js APIs, and rendering JSX without client-side hooks.
DReact hooks like useState and useEffect for managing state and lifecycle.
Attempts:
2 left
💡 Hint
Think about what runs on the server vs what runs in the browser.
state_output
expert
3:00remaining
What is the output of this mixed Server and Client Component setup?
Given the code below, what will be rendered on the page?
NextJS
import ClientCounter from './ClientCounter';

export default async function Page() {
  const message = await Promise.resolve('Welcome');
  return (
    <>
      <h1>{message}</h1>
      <ClientCounter />
    </>
  );
}

// ClientCounter.jsx
'use client';
import { useState } from 'react';
export default function ClientCounter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
A<h1>Welcome</h1> and a button showing 'Count: 0' that increments on click
BError: Cannot use useState in Server Component
C<h1>Welcome</h1> and a static button showing 'Count: 0' with no interaction
D<h1>{message}</h1> and a button showing 'Count: 0'
Attempts:
2 left
💡 Hint
Server Components can render Client Components which handle state and events.