Learn/Next.js/Chapter 10

Chapter 10 of 21

Continue learning Next.js step by step.

48%
Chapter 10

Data Fetching

Learn how to fetch data in Next.js applications.

Theory

Next.js supports data fetching in Server Components. Data can be fetched from APIs, databases and other services. Fetching data on the server can reduce client-side JavaScript and improve application performance.

Example

export default async function UsersPage() {
  const response = await fetch(
    "https://api.example.com/users"
  );

  const users = await response.json();

  return (
    <div>
      {users.map((user: any) => (
        <p key={user.id}>{user.name}</p>
      ))}
    </div>
  );
}

Output

The page displays users returned by the API.

Practice

Fetch data from an API and display it in a Next.js page.

Interview Questions

1

How can data be fetched in Next.js?

2

Why is server-side data fetching useful?

3

Can Server Components use async functions?

Quick Quiz

Which option best describes this chapter?

A.Server-side data fetching
B.Only CSS fetching
C.Database-only rendering
D.No data fetching