Bird
Raised Fist0
NextJSframework~20 mins

Data fetching in server components in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Server Data Fetching Master
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 server component fetching data?
Consider this Next.js server component fetching user data. What will it render?
NextJS
import React from 'react';

async function getUser() {
  return { name: 'Alice', age: 30 };
}

export default async function User() {
  const user = await getUser();
  return <div>{`Name: ${user.name}, Age: ${user.age}`}</div>;
}
A<div>Name: Alice, Age: 30</div>
B<div>Name: undefined, Age: undefined</div>
CError: Cannot use await in server component
D<div>Name: , Age: 0</div>
Attempts:
2 left
💡 Hint
Server components can use async/await directly for data fetching.
📝 Syntax
intermediate
2:00remaining
Which option correctly fetches data in a Next.js server component?
Identify the correct way to fetch data inside a Next.js server component.
A
export default function Page() {
  const data = fetch('/api/data').then(res =&gt; res.json());
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
B
export default async function Page() {
  const data = fetch('https://api.example.com/data').json();
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
C
export default function Page() {
  const data = useEffect(() =&gt; {
    fetch('/api/data').then(res =&gt; res.json());
  });
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
D
export default async function Page() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
Attempts:
2 left
💡 Hint
Server components support async/await and fetch API directly.
🔧 Debug
advanced
2:00remaining
Why does this server component cause a runtime error?
Examine this Next.js server component code. Why does it throw an error at runtime?
NextJS
export default function Page() {
  const data = await fetch('https://api.example.com/data').then(res => res.json());
  return <div>{data.name}</div>;
}
AReferenceError: data is not defined
BTypeError: fetch is not defined
CSyntaxError: 'await' is only valid in async functions
DNo error, renders data.name correctly
Attempts:
2 left
💡 Hint
Check if the function is declared async when using await.
❓ state_output
advanced
2:00remaining
What is the rendered output of this server component with conditional data fetching?
Given this Next.js server component, what will it render?
NextJS
async function getData(flag) {
  if (flag) {
    return { message: 'Flag is true' };
  } else {
    return null;
  }
}

export default async function Page() {
  const data = await getData(false);
  return <div>{data?.message ?? 'No data available'}</div>;
}
A<div>Flag is true</div>
B<div>No data available</div>
C<div>undefined</div>
DError: Cannot read property 'message' of null
Attempts:
2 left
💡 Hint
Optional chaining and nullish coalescing handle null data safely.
🧠 Conceptual
expert
2:00remaining
Which statement about data fetching in Next.js server components is TRUE?
Select the correct statement regarding data fetching in Next.js server components.
AServer components can fetch data directly using async/await and the fetch API without client-side JavaScript.
BData fetching in server components happens at build time only and cannot be dynamic.
CServer components can use React hooks like useState and useEffect for data fetching.
DServer components must always fetch data via client components to avoid blocking rendering.
Attempts:
2 left
💡 Hint
Think about how server components handle data fetching compared to client components.

Practice

(1/5)
1. What is the main advantage of fetching data inside Next.js server components?
easy
A. It requires no network requests at all.
B. Data is fetched before rendering, improving page speed and SEO.
C. It allows using React hooks like useState for data fetching.
D. Data fetching happens on the client side for faster UI updates.

Solution

  1. Step 1: Understand server component data fetching

    Server components fetch data before rendering, so the page is ready with data when sent to the browser.
  2. Step 2: Recognize benefits for speed and SEO

    This pre-rendering with data improves page load speed and helps search engines index content better.
  3. Final Answer:

    Data is fetched before rendering, improving page speed and SEO. -> Option B
  4. Quick Check:

    Server components fetch data early = D [OK]
Hint: Server components fetch data before render for speed and SEO [OK]
Common Mistakes:
  • Thinking data fetching happens on client side in server components
  • Confusing server components with client components
  • Believing no network requests are needed
2. Which of the following is the correct way to fetch data inside a Next.js server component?
easy
A. const data = fetch('https://api.example.com/data', { method: 'POST' });
B. const data = fetch('https://api.example.com/data').then(res => res.json());
C. useEffect(() => { fetch('https://api.example.com/data'); }, []);
D. const data = await fetch('https://api.example.com/data');

Solution

  1. Step 1: Identify server component data fetching syntax

    Server components support top-level await, so you can directly use await fetch().
  2. Step 2: Check each option

    const data = await fetch('https://api.example.com/data'); uses await fetch correctly. const data = fetch('https://api.example.com/data').then(res => res.json()); assigns a Promise to data without awaiting. useEffect(() => { fetch('https://api.example.com/data'); }, []); uses useEffect, which is client-side only. const data = fetch('https://api.example.com/data', { method: 'POST' }); uses POST without context, likely incorrect for simple fetch.
  3. Final Answer:

    const data = await fetch('https://api.example.com/data'); -> Option D
  4. Quick Check:

    Top-level await fetch in server components = A [OK]
Hint: Use top-level await fetch() in server components [OK]
Common Mistakes:
  • Using useEffect inside server components
  • Not using await with fetch
  • Confusing client-side hooks with server code
3. Given this server component code, what will be rendered?
export default async function Page() {
  const res = await fetch('https://api.example.com/user');
  const user = await res.json();
  return <h1>Hello, {user.name}!</h1>;
}
medium
A. <h1>Hello, John!</h1>
B. Error: fetch not allowed in server components
C. <h1>Hello, undefined!</h1>
D. Loading spinner until data arrives

Solution

  1. Step 1: Understand server component data fetching

    The fetch call waits for the API response, then parses JSON to get user data.
  2. Step 2: Render with fetched data

    The component returns an h1 element greeting the user by name, assuming user.name is 'John'.
  3. Final Answer:

    <h1>Hello, John!</h1> -> Option A
  4. Quick Check:

    Server component fetch + render data = C [OK]
Hint: Server components await fetch, render data directly [OK]
Common Mistakes:
  • Expecting client-side loading states in server components
  • Assuming fetch is disallowed in server components
  • Not awaiting fetch response before rendering
4. Identify the error in this Next.js server component fetching data:
export default function Page() {
  const data = await fetch('https://api.example.com/data');
  return <div>Data loaded</div>;
}
medium
A. Missing JSON parsing of fetch response.
B. fetch is not allowed in server components.
C. Cannot use await inside a non-async function.
D. Return statement must be inside useEffect.

Solution

  1. Step 1: Check function declaration

    The function is not marked async but uses await, which is invalid syntax.
  2. Step 2: Understand correct usage

    To use await, the function must be async. fetch is allowed in server components, and JSON parsing is optional depending on usage.
  3. Final Answer:

    Cannot use await inside a non-async function. -> Option C
  4. Quick Check:

    await requires async function = A [OK]
Hint: Mark function async to use await inside it [OK]
Common Mistakes:
  • Forgetting async keyword on server component
  • Thinking fetch is disallowed in server components
  • Misusing client hooks like useEffect in server components
5. You want to fetch user data and posts in a Next.js server component and display the user's name with the number of posts. Which code correctly fetches both and renders the result?
export default async function Page() {
  const userRes = await fetch('https://api.example.com/user');
  const postsRes = await fetch('https://api.example.com/posts');
  const user = await userRes.json();
  const posts = await postsRes.json();
  return <div>{user.name} has {posts.length} posts.</div>;
}
hard
A. Correctly fetches both and renders user name with posts count.
B. fetch calls must be inside useEffect for server components.
C. Cannot fetch multiple resources in one server component.
D. Must combine fetch calls with Promise.all to work.

Solution

  1. Step 1: Analyze fetch calls in server component

    Server components support multiple await fetch calls sequentially as shown.
  2. Step 2: Confirm rendering logic

    The component renders user.name and posts.length correctly after awaiting JSON parsing.
  3. Final Answer:

    Correctly fetches both and renders user name with posts count. -> Option A
  4. Quick Check:

    Multiple awaits in server component allowed = B [OK]
Hint: Await multiple fetches sequentially in async server component [OK]
Common Mistakes:
  • Thinking useEffect is needed in server components
  • Believing multiple fetch calls are disallowed
  • Assuming Promise.all is required for correctness
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