0
0
NextJSframework~20 mins

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

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