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
Fetch Data in Next.js Server Components
📖 Scenario: You are building a simple Next.js app that shows a list of users fetched from an API. You want to fetch the data directly in a server component to keep the UI fast and simple.
🎯 Goal: Build a Next.js server component that fetches user data from an API and displays the user names in a list.
📋 What You'll Learn
Create a server component file named UsersList.jsx.
Fetch user data from https://jsonplaceholder.typicode.com/users using fetch inside the server component.
Store the fetched data in a variable called users.
Render a list of user names inside an unordered list <ul>.
Use only server component features (no client-side hooks or effects).
💡 Why This Matters
🌍 Real World
Fetching data in server components is common in Next.js apps to improve performance and SEO by loading data on the server before sending HTML to the browser.
💼 Career
Understanding server components and data fetching is essential for modern React and Next.js development roles, enabling you to build fast, scalable web apps.
Progress0 / 4 steps
1
Create the Server Component File
Create a new file named UsersList.jsx and define a default exported async function component called UsersList.
NextJS
Hint
Start by writing an async function named UsersList and export it as default.
2
Fetch User Data from the API
Inside the UsersList function, use fetch to get data from https://jsonplaceholder.typicode.com/users and store the JSON response in a variable called users.
NextJS
Hint
Use await fetch(url) and then await response.json() to get the data.
3
Render the List of User Names
Return JSX that renders an unordered list <ul>. Inside it, use users.map with user as the iterator to create list items <li> showing each user.name. Use user.id as the key for each list item.
NextJS
Hint
Use return ( <ul> {users.map(user => <li key={user.id}>{user.name}</li>)} </ul> ).
4
Add Fetch Options to Disable Caching
Modify the fetch call by adding a second argument object with cache: 'no-store' to ensure fresh data is fetched on every request.
NextJS
Hint
Add { cache: 'no-store' } as the second argument to fetch.
Practice
(1/5)
1. In Next.js server components, where should you place the fetch() call to get data before rendering?
easy
A. In a separate API route only
B. Inside a client-side useEffect hook
C. Directly inside the async server component using await fetch()
D. Inside a React state hook like useState
Solution
Step 1: Understand server components run on the server
Server components execute on the server before sending HTML to the browser, so data fetching happens here.
Step 2: Use await fetch() inside async server components
Using await fetch() directly inside an async server component fetches data before rendering.
Final Answer:
Directly inside the async server component using await fetch() -> Option C
Quick Check:
Fetch in async server component = Directly inside the async server component using await fetch() [OK]
Hint: Fetch data inside async server components with await fetch() [OK]
Common Mistakes:
Trying to fetch data inside client hooks like useEffect
Fetching data in React state hooks which run on client
Assuming API routes are mandatory for server component fetch
2. Which of the following is the correct syntax to fetch data inside a Next.js server component?
easy
A. const data = await fetch('https://api.example.com/data').then(res => res.json())
B. const data = fetch('https://api.example.com/data')
C. const data = await fetch('https://api.example.com/data').json()
D. const data = fetch('https://api.example.com/data').json()
Solution
Step 1: Use await with fetch to wait for response
Fetch returns a promise, so use await to wait for it to resolve.
Step 2: Call res.json() on the response to parse JSON
After fetch resolves, call res.json() to get the parsed data, also awaited or chained with then.
Final Answer:
const data = await fetch('https://api.example.com/data').then(res => res.json()) -> Option A
Quick Check:
Await fetch then parse JSON = const data = await fetch('https://api.example.com/data').then(res => res.json()) [OK]
Hint: Always await fetch and then parse JSON with res.json() [OK]
Common Mistakes:
Forgetting to await fetch causing unresolved promises
Calling .json() directly on fetch without awaiting response
Using fetch without handling the response parsing
3. What will be the output of this Next.js server component code?
export default async function Page() {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const todo = await res.json();
return
{JSON.stringify(todo)}
;
}
medium
A.
{"userId":1,"id":1,"title":"delectus aut autem","completed":false}
B. SyntaxError because fetch cannot be used in server components
C. Empty output because data is not awaited
D. Loading spinner indefinitely because fetch is async
Solution
Step 1: Await fetch and parse JSON in server component
The code correctly awaits fetch and then awaits res.json() to get the todo object.
Step 2: Return JSON stringified todo inside <pre> tag
The component returns a <pre> element with the stringified todo data, so it renders the JSON text.
Final Answer:
<pre>{"userId":1,"id":1,"title":"delectus aut autem","completed":false}</pre> -> Option A
Quick Check:
Await fetch + JSON.stringify output =
{"userId":1,"id":1,"title":"delectus aut autem","completed":false}
[OK]
Hint: Await fetch and JSON parse before returning JSX [OK]
Common Mistakes:
Expecting fetch to fail in server components
Not awaiting fetch causing unresolved promises
Returning raw object instead of stringified JSON
4. Identify the error in this Next.js server component fetching code:
export default function Page() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return <div>{data.title}</div>;
}
medium
A. res.json() does not return a promise
B. Missing async keyword on the component function
C. fetch cannot be used in server components
D. JSX syntax error in return statement
Solution
Step 1: Check async usage in server component
The component uses await but is not declared async, which causes a syntax error.
Step 2: Confirm fetch is allowed in server components
Fetch is allowed in server components, so no error there. Also, res.json() returns a promise, so awaiting is correct.
Final Answer:
Missing async keyword on the component function -> Option B
Quick Check:
Await requires async function = Missing async keyword on the component function [OK]
Hint: Add async to function using await fetch [OK]
Common Mistakes:
Forgetting async on function using await
Thinking fetch is disallowed in server components
Misunderstanding res.json() returns a promise
5. You want to fetch user data and posts in a Next.js server component and display the user's name with their post titles. Which approach correctly fetches both and renders them efficiently?
hard
A. Fetch posts first, then fetch user data inside a nested fetch call
B. Fetch user data in server component and posts in client component using useEffect
C. Fetch user and posts sequentially with two awaits, then render after both complete
D. Fetch user and posts in parallel using Promise.all inside the server component