📖 Scenario: You are building a simple Next.js app that shows a list of users fetched from an API. You will use server components to fetch the data on the server side and render it directly.
🎯 Goal: Create a Next.js server component that fetches user data from an API and displays the users' names in a list.
📋 What You'll Learn
Create a server component named UsersList
Fetch data from https://jsonplaceholder.typicode.com/users
Store the fetched data in a variable called users
Render a list of user names inside an unordered list <ul>
Use async and await for data fetching
Export the component as default
💡 Why This Matters
🌍 Real World
Fetching data on the server side improves performance and SEO by sending fully rendered pages to the browser. This is common in modern web apps.
💼 Career
Understanding server components and data fetching is essential for Next.js developers building fast, scalable web applications.
Progress0 / 4 steps
1
Create the server component and import React
Create a new file named UsersList.jsx. Inside it, write a React server component named UsersList that is an async function. Start by importing React from "react".
NextJS
Hint
Remember, server components can be async functions in Next.js.
2
Fetch user data from the API
Inside the UsersList component, create a constant variable named response that fetches data from https://jsonplaceholder.typicode.com/users using await fetch(). Then create another constant named users that awaits response.json() to parse the JSON data.
NextJS
Hint
Use await before fetch and response.json() to wait for the data.
3
Render the list of user names
Inside the UsersList component, return a JSX fragment that contains an unordered list <ul>. Use users.map() with user as the iterator variable to create a list item <li> for each user. Display the user's name inside each <li>. Add a unique key prop to each <li> using user.id.
NextJS
Hint
Use users.map() to create list items and remember to add a key prop.
4
Complete and export the server component
Ensure the UsersList component is exported as the default export. The component should be a fully working server component that fetches and displays the user names in a list.
NextJS
Hint
The component must be exported as default to be used in Next.js pages.
Practice
(1/5)
1. What is the main advantage of fetching data inside Next.js server components?
easy
A. It requires no network requests at all.
B. Data is fetched before rendering, improving page speed and SEO.
C. It allows using React hooks like useState for data fetching.
D. Data fetching happens on the client side for faster UI updates.
Solution
Step 1: Understand server component data fetching
Server components fetch data before rendering, so the page is ready with data when sent to the browser.
Step 2: Recognize benefits for speed and SEO
This pre-rendering with data improves page load speed and helps search engines index content better.
Final Answer:
Data is fetched before rendering, improving page speed and SEO. -> Option B
Quick Check:
Server components fetch data early = D [OK]
Hint: Server components fetch data before render for speed and SEO [OK]
Common Mistakes:
Thinking data fetching happens on client side in server components
Confusing server components with client components
Believing no network requests are needed
2. Which of the following is the correct way to fetch data inside a Next.js server component?
easy
A. const data = fetch('https://api.example.com/data', { method: 'POST' });
B. const data = fetch('https://api.example.com/data').then(res => res.json());
C. useEffect(() => { fetch('https://api.example.com/data'); }, []);
D. const data = await fetch('https://api.example.com/data');
Solution
Step 1: Identify server component data fetching syntax
Server components support top-level await, so you can directly use await fetch().
Step 2: Check each option
const data = await fetch('https://api.example.com/data'); uses await fetch correctly. const data = fetch('https://api.example.com/data').then(res => res.json()); assigns a Promise to data without awaiting. useEffect(() => { fetch('https://api.example.com/data'); }, []); uses useEffect, which is client-side only. const data = fetch('https://api.example.com/data', { method: 'POST' }); uses POST without context, likely incorrect for simple fetch.
Final Answer:
const data = await fetch('https://api.example.com/data'); -> Option D
Quick Check:
Top-level await fetch in server components = A [OK]
Hint: Use top-level await fetch() in server components [OK]
Common Mistakes:
Using useEffect inside server components
Not using await with fetch
Confusing client-side hooks with server code
3. Given this server component code, what will be rendered?
export default async function Page() {
const res = await fetch('https://api.example.com/user');
const user = await res.json();
return <h1>Hello, {user.name}!</h1>;
}
medium
A. <h1>Hello, John!</h1>
B. Error: fetch not allowed in server components
C. <h1>Hello, undefined!</h1>
D. Loading spinner until data arrives
Solution
Step 1: Understand server component data fetching
The fetch call waits for the API response, then parses JSON to get user data.
Step 2: Render with fetched data
The component returns an h1 element greeting the user by name, assuming user.name is 'John'.
Final Answer:
<h1>Hello, John!</h1> -> Option A
Quick Check:
Server component fetch + render data = C [OK]
Hint: Server components await fetch, render data directly [OK]
Common Mistakes:
Expecting client-side loading states in server components
Assuming fetch is disallowed in server components
Not awaiting fetch response before rendering
4. Identify the error in this Next.js server component fetching data:
export default function Page() {
const data = await fetch('https://api.example.com/data');
return <div>Data loaded</div>;
}
medium
A. Missing JSON parsing of fetch response.
B. fetch is not allowed in server components.
C. Cannot use await inside a non-async function.
D. Return statement must be inside useEffect.
Solution
Step 1: Check function declaration
The function is not marked async but uses await, which is invalid syntax.
Step 2: Understand correct usage
To use await, the function must be async. fetch is allowed in server components, and JSON parsing is optional depending on usage.
Final Answer:
Cannot use await inside a non-async function. -> Option C
Quick Check:
await requires async function = A [OK]
Hint: Mark function async to use await inside it [OK]
Common Mistakes:
Forgetting async keyword on server component
Thinking fetch is disallowed in server components
Misusing client hooks like useEffect in server components
5. You want to fetch user data and posts in a Next.js server component and display the user's name with the number of posts. Which code correctly fetches both and renders the result?