0
0
NextJSframework~20 mins

Why data fetching differs in Next.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Data Fetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Server Components and Data Fetching
In Next.js 14+, how does using Server Components affect data fetching compared to Client Components?
AServer Components do not support data fetching at all and rely on static props.
BServer Components fetch data only after the page loads on the client, causing slower initial render.
CServer Components fetch data on the server during rendering, reducing client bundle size and improving performance.
DServer Components require manual client-side fetching with useEffect to get data.
Attempts:
2 left
💡 Hint
Think about where the code runs and when data is fetched.
component_behavior
intermediate
2: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>;
}
AData is fetched after the component mounts on the client, causing a loading state initially.
BData is fetched on the server before rendering, so no loading state appears.
CData fetching fails because useEffect does not run in Next.js.
DData is fetched during build time and never updates on the client.
Attempts:
2 left
💡 Hint
Remember when useEffect runs in React components.
📝 Syntax
advanced
2:00remaining
Correct Data Fetching in Next.js Server Component
Which code snippet correctly fetches data in a Next.js Server Component using async/await?
A
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;;
}
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 = fetch('https://api.example.com/data').json();
  return &lt;div&gt;{data.title}&lt;/div&gt;;
}
D
export default function Page() {
  fetch('https://api.example.com/data').then(res =&gt; res.json()).then(data =&gt; {
    return &lt;div&gt;{data.title}&lt;/div&gt;;
  });
}
Attempts:
2 left
💡 Hint
Check for proper async/await usage and returning JSX.
🔧 Debug
advanced
2: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...'}
; }
ARuntime error because fetch is not defined in client components.
BSyntaxError because useEffect callback cannot be async directly.
CNo error; data fetches correctly and displays name.
DTypeError because setData is called with undefined.
Attempts:
2 left
💡 Hint
Consider how useEffect expects its callback function.
state_output
expert
3: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...'}

; }
ANo output because Client Component cannot render inside Server Component.
B<p>Hello from client</p> immediately because Server Component preloads state.
C<p></p> empty paragraph because initial state is empty string.
D<p>Loading...</p> because state is initially empty and updates after 1 second.
Attempts:
2 left
💡 Hint
Think about when useEffect runs and initial state values.