0
0
NextJSframework~20 mins

Server component restrictions 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 happens if a Server Component tries to use useState?
Consider a Next.js Server Component that calls the React hook useState. What will happen when this component is rendered?
NextJS
import React, { useState } from 'react';

export default function ServerComp() {
  const [count, setCount] = useState(0);
  return <div>{count}</div>;
}
AThe component will throw a runtime error because useState is not allowed in Server Components.
BThe component will render normally and update state on user interaction.
CThe component will render but the state will never update.
DThe component will compile but cause a hydration mismatch warning in the browser.
Attempts:
2 left
💡 Hint
Remember that Server Components run only on the server and cannot use client-only hooks.
📝 Syntax
intermediate
1:30remaining
Which import statement is invalid in a Next.js Server Component?
Identify the import statement that will cause an error when used inside a Next.js Server Component.
Aimport fs from 'fs';
Bimport { useEffect } from 'react';
Cimport fetch from 'node-fetch';
Dimport path from 'path';
Attempts:
2 left
💡 Hint
Think about which imports are client-only React hooks.
🔧 Debug
advanced
2:30remaining
Why does this Server Component cause a hydration mismatch?
Given this Server Component code, why does the browser show a hydration mismatch warning?
NextJS
export default function ServerComp() {
  const date = new Date().toLocaleTimeString();
  return <div>{date}</div>;
}
ABecause Server Components cannot return JSX elements.
BBecause the component uses a client hook inside a Server Component.
CBecause the date string changes on each render causing server and client output to differ.
DBecause the component is missing a key prop on the root element.
Attempts:
2 left
💡 Hint
Think about what changes between server render and client hydration.
🧠 Conceptual
advanced
2:00remaining
Which of these is NOT allowed inside a Next.js Server Component?
Select the operation that is disallowed inside a Next.js Server Component.
AUsing React hooks like useState or useEffect.
BFetching data from an external API using fetch.
CUsing React Context to share state between components.
DReading files from the local filesystem using Node.js APIs.
Attempts:
2 left
💡 Hint
Recall which React features are client-only.
state_output
expert
3:00remaining
What is the value of count after rendering this Server Component?
Analyze the following Server Component code and determine the value of count when rendered.
NextJS
let count = 0;

export default function ServerComp() {
  count += 1;
  return <div>{count}</div>;
}
Acount will always be 0 because Server Components cannot modify variables.
Bcount will cause a runtime error because of variable mutation in Server Components.
Ccount will be 1 on every render because Server Components run fresh each time.
Dcount will increment on every request and keep increasing indefinitely.
Attempts:
2 left
💡 Hint
Server Components' modules are cached like any Node.js module. Top-level state persists across requests.