Bird
Raised Fist0
NextJSframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Why does Next.js use different methods for data fetching?
easy
A. Because Next.js only works with static data
B. Because Next.js does not support client-side data fetching
C. Because all data must be fetched only on the server
D. Because data can be fetched at different times for better performance

Solution

  1. Step 1: Understand data fetching timing in Next.js

    Next.js allows fetching data before the page loads (server-side or static) or after the page loads (client-side).
  2. Step 2: Recognize the reason for multiple methods

    Using different methods helps improve performance and user experience by choosing the best time to fetch data.
  3. Final Answer:

    Because data can be fetched at different times for better performance -> Option D
  4. Quick Check:

    Data fetching timing = A [OK]
Hint: Think when data is needed: before or after page loads [OK]
Common Mistakes:
  • Assuming Next.js only fetches data on the server
  • Believing client-side fetching is not supported
  • Thinking all data must be static
2. Which of the following is the correct way to fetch data at build time in Next.js?
easy
A. export async function getStaticProps() { return { props: {} } }
B. export async function getServerSideProps() { return { props: {} } }
C. export async function fetchData() { return { props: {} } }
D. export async function getClientSideProps() { return { props: {} } }

Solution

  1. Step 1: Identify build-time data fetching method

    Next.js uses getStaticProps to fetch data at build time for static generation.
  2. Step 2: Compare options with Next.js conventions

    getServerSideProps is for server-side rendering, not build time; others are invalid function names.
  3. Final Answer:

    export async function getStaticProps() { return { props: {} } } -> Option A
  4. Quick Check:

    Build-time fetch = getStaticProps A [OK]
Hint: Remember: Static = getStaticProps, Server = getServerSideProps [OK]
Common Mistakes:
  • Confusing getServerSideProps with getStaticProps
  • Using incorrect function names
  • Thinking client-side fetching uses special props functions
3. What will be the rendered output if you use getServerSideProps to fetch data that changes every second?
medium
A. The page shows the data as it was at build time
B. The page never updates after first load
C. The page shows the latest data on every request
D. The page shows an error because data changes too fast

Solution

  1. Step 1: Understand getServerSideProps behavior

    This function runs on every request, so it fetches fresh data each time the page loads.
  2. Step 2: Apply to data changing every second

    Since data changes frequently, getServerSideProps ensures the page always shows the latest data.
  3. Final Answer:

    The page shows the latest data on every request -> Option C
  4. Quick Check:

    Server-side fetch = fresh data B [OK]
Hint: Server-side fetch updates on every request [OK]
Common Mistakes:
  • Thinking getServerSideProps caches data at build time
  • Assuming data never updates after first load
  • Believing it causes errors with fast-changing data
4. Identify the error in this Next.js data fetching code:
export async function getStaticProps() {
  const res = await fetch('https://api.example.com/data')
  const data = await res.json()
  return { data }
}
medium
A. Missing return of props object wrapping data
B. fetch cannot be used inside getStaticProps
C. Async functions are not allowed in Next.js data fetching
D. The URL must be relative, not absolute

Solution

  1. Step 1: Check return value format in getStaticProps

    Next.js expects an object with a props key containing the data, not just data alone.
  2. Step 2: Identify the missing wrapper

    The code returns { data } but should return { props: { data } } for Next.js to pass props correctly.
  3. Final Answer:

    Missing return of props object wrapping data -> Option A
  4. Quick Check:

    Return props object = C [OK]
Hint: Always return { props: { ... } } in getStaticProps [OK]
Common Mistakes:
  • Returning data directly without props wrapper
  • Thinking fetch is disallowed in getStaticProps
  • Believing async functions are forbidden
5. You want to show user-specific data that updates frequently but also want fast initial page load in Next.js. Which approach best fits this need?
hard
A. Use getStaticProps to fetch data and revalidate every second
B. Use getStaticProps for static data and fetch user data client-side after load
C. Fetch all data client-side only after page loads
D. Use getServerSideProps to fetch data on every request

Solution

  1. Step 1: Analyze requirements for fast initial load and frequent updates

    Static data can be fetched at build time for fast load; user-specific data changes often and should be fetched client-side.
  2. Step 2: Evaluate options

    Use getStaticProps to fetch data and revalidate every second revalidates too frequently and may cause performance issues; Use getServerSideProps to fetch data on every request delays initial load; Fetch all data client-side only after page loads delays all data; Use getStaticProps for static data and fetch user data client-side after load balances fast load and fresh user data.
  3. Final Answer:

    Use getStaticProps for static data and fetch user data client-side after load -> Option B
  4. Quick Check:

    Static + client fetch = D [OK]
Hint: Combine static build with client fetch for user data [OK]
Common Mistakes:
  • Using only server-side fetching causing slow load
  • Fetching everything client-side causing blank initial page
  • Overusing revalidation causing unnecessary server load