Challenge - 5 Problems
Next.js Data Fetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding Server Components and Data Fetching
In Next.js 14+, how does using Server Components affect data fetching compared to Client Components?
Attempts:
2 left
💡 Hint
Think about where the code runs and when data is fetched.
✗ Incorrect
Server Components run on the server and can fetch data directly during rendering. This means less JavaScript is sent to the browser, improving load speed.
❓ component_behavior
intermediate2:00remaining
Effect of useEffect on Data Fetching in Next.js
What happens if you fetch data inside a Client Component using useEffect in Next.js?
NextJS
import { useState, useEffect } from 'react'; export default function ClientData() { const [data, setData] = useState(null); useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []); return <div>{data ? data.message : 'Loading...'}</div>; }
Attempts:
2 left
💡 Hint
Remember when useEffect runs in React components.
✗ Incorrect
useEffect runs only on the client after the component mounts, so data fetching happens after initial render, showing a loading state first.
📝 Syntax
advanced2:00remaining
Correct Data Fetching in Next.js Server Component
Which code snippet correctly fetches data in a Next.js Server Component using async/await?
Attempts:
2 left
💡 Hint
Check for proper async/await usage and returning JSX.
✗ Incorrect
Option A correctly uses async/await to fetch and parse JSON before returning JSX. Other options misuse promises or omit await.
🔧 Debug
advanced2:00remaining
Identifying Data Fetching Error in Next.js Client Component
What error will this Next.js Client Component cause when fetching data, and why?
import { useState, useEffect } from 'react';
export default function Data() {
const [data, setData] = useState(null);
useEffect(async () => {
const res = await fetch('/api/data');
const json = await res.json();
setData(json);
}, []);
return
{data ? data.name : 'Loading...'}
;
}Attempts:
2 left
💡 Hint
Consider how useEffect expects its callback function.
✗ Incorrect
useEffect expects a synchronous function. Declaring the callback async returns a promise, which React does not handle, causing a syntax error.
❓ state_output
expert3:00remaining
State Behavior with Mixed Server and Client Components in Next.js
Given a Next.js page with a Server Component rendering a Client Component that fetches data, what will be the initial rendered output before data loads?
Server Component:
export default function Page() {
return ;
}
Client Component:
import { useState, useEffect } from 'react';
export default function ClientData() {
const [msg, setMsg] = useState('');
useEffect(() => {
setTimeout(() => setMsg('Hello from client'), 1000);
}, []);
return
{msg || 'Loading...'}
; }Attempts:
2 left
💡 Hint
Think about when useEffect runs and initial state values.
✗ Incorrect
The Client Component starts with empty state, so it shows 'Loading...'. After 1 second, useEffect updates the state to show the message.