Bird
Raised Fist0
NextJSframework~20 mins

What can run in server components in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following can you safely run inside a Next.js server component?
easy
A. Manipulating the DOM directly
B. Accessing a database directly
C. Adding event listeners for user clicks
D. Using browser APIs like localStorage

Solution

  1. Step 1: Understand server component environment

    Server components run only on the server, so they can use server-only features like database access.
  2. Step 2: Identify client-only features

    Browser APIs, event listeners, and DOM manipulation require client-side JavaScript and cannot run in server components.
  3. Final Answer:

    Accessing a database directly -> Option B
  4. Quick Check:

    Server-only code = Access database [OK]
Hint: Server components run server code only, no browser APIs [OK]
Common Mistakes:
  • Thinking client APIs like localStorage work in server components
  • Trying to add event listeners in server components
  • Assuming DOM manipulation is possible on server
2. Which syntax correctly imports a server-only module inside a Next.js server component?
easy
A. import localStorage from 'localStorage';
B. import { useState } from 'react';
C. import window from 'window';
D. import fs from 'fs';

Solution

  1. Step 1: Recognize server-only modules

    Modules like 'fs' (file system) are server-only and can be imported in server components.
  2. Step 2: Identify invalid imports

    useState is client-side React hook, window and localStorage are browser globals, not importable modules.
  3. Final Answer:

    import fs from 'fs'; -> Option D
  4. Quick Check:

    Server module import = fs [OK]
Hint: Only import Node.js modules like fs in server components [OK]
Common Mistakes:
  • Importing client hooks like useState in server components
  • Trying to import browser globals as modules
  • Confusing client and server imports
3. Given this server component code, what will be rendered?
import fs from 'fs';
export default function Page() {
  const data = fs.readFileSync('data.txt', 'utf8');
  return <div>{data}</div>;
}
medium
A. Contents of data.txt file inside a div
B. Error: fs module not found
C. Empty div with no content
D. Client-side error about fs usage

Solution

  1. Step 1: Understand server component execution

    The code runs on the server, so fs.readFileSync reads the file content successfully.
  2. Step 2: Render the file content

    The content read from 'data.txt' is returned inside a div and sent as HTML to the client.
  3. Final Answer:

    Contents of data.txt file inside a div -> Option A
  4. Quick Check:

    Server read file = content rendered [OK]
Hint: Server components can read files and render content directly [OK]
Common Mistakes:
  • Thinking fs is unavailable in server components
  • Expecting client-side errors for server code
  • Assuming empty output without file content
4. What is wrong with this server component code?
export default function Page() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
medium
A. Both A and B
B. onClick handlers are not allowed in server components
C. useState cannot be used in server components
D. No error, code works fine

Solution

  1. Step 1: Check React hooks usage in server components

    useState is a client-side hook and cannot be used in server components.
  2. Step 2: Check event handlers in server components

    onClick handlers require client-side JavaScript and are not supported in server components.
  3. Final Answer:

    Both A and B -> Option A
  4. Quick Check:

    useState + onClick invalid in server components [OK]
Hint: No React hooks or event handlers in server components [OK]
Common Mistakes:
  • Using useState in server components
  • Adding client event handlers in server components
  • Assuming server components can handle user events
5. You want to fetch data from a database and display it in a Next.js app. Which approach best uses server components to improve performance and security?
hard
A. Fetch data in the browser using fetch API directly
B. Fetch data inside a client component using useEffect
C. Fetch data inside a server component and pass it as props to a client component
D. Fetch data in a client component and store it in localStorage

Solution

  1. Step 1: Understand server component advantages

    Server components can securely fetch data on the server, avoiding exposing secrets or extra client JS.
  2. Step 2: Use server component to fetch and pass data

    Fetching data in server component and passing it as props to client components optimizes performance and security.
  3. Final Answer:

    Fetch data inside a server component and pass it as props to a client component -> Option C
  4. Quick Check:

    Server fetch + props passing = best practice [OK]
Hint: Fetch data server-side, then pass to client components [OK]
Common Mistakes:
  • Fetching data only on client causing slower load
  • Exposing secrets by fetching in client
  • Storing sensitive data in localStorage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard