Fullstack CourseLearn by building
Back to week 3

Topic

Fetch data in Next.js

Definition

Server Components can await rendering data on the server, while interactive browser views synchronize remote server state through a client cache such as TanStack Query.

In simpler words

Fetch where the data is needed: server rendering can await it; interactive Nest-backed UI uses Query in the browser.

Fetching strategy follows rendering and ownership requirements, not a desire to replace the product API.

After this you can

  • Choose a server or client data path
  • Use Query for interactive Nest data
  • Keep product API ownership in Nest

Match fetching to the UI boundary

A Server Component can await data before it returns UI, which is useful when the initial page representation needs that data. Client components should not recreate ad hoc effect-based loading for product server state when Query already owns the request lifecycle.

TanStack Query supplies loading, error, cache, retry, and invalidation behavior for interactive ticket views. Query keys must include every value that changes the result, such as a status filter or page number.

A client ticket query

'use client';
const ticketsQuery = useQuery({
  queryKey: ['tickets', { status }],
  queryFn: () => ticketsApi.list({ status }),
});

if (ticketsQuery.isLoading) return <Spinner />;

The key and query function describe the same filtered resource.

Mistake: fetching Nest tickets in useEffect

// Wrong — hand-rolls loading, errors, and stale data
useEffect(() => { ticketsApi.list().then(setTickets); }, []);

// Right — use the established Query layer
useQuery({ queryKey: ['tickets'], queryFn: ticketsApi.list });

Query centralizes remote-state synchronization and refresh behavior.

Keep data ownership clear

Definition

The renderer chooses how to obtain a representation; the backend still owns the product rules and authoritative records.

In simpler words

Next is the web application, while Nest remains the tickets and authentication API.

Use a server-side fetch only with the intended credentials and deployment topology in mind.

Never treat data received by the UI as authorization to perform the corresponding write.

Live playground

Fetching data playground

Change a query key and predict whether Query treats it as a new result.

Interactive tickets/Pulse lists: Axios + Query with credentials.

Keep in mind

  • Use Query keys that include filters and pagination.
  • Show explicit loading, error, and empty states.
  • Keep Nest as the product API even when Next renders data.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement is the most accurate definition of fetching data in Next?
Syntax2. Which code-level choice is consistent with fetching data in Next?
Practical3. A reviewer sees this situation. Which decision best applies fetching data in Next?
Logic4. What reasoning correctly explains why this approach works for fetching data in Next?
Concept5. Which boundary does the correct use of fetching data in Next preserve?
Practical6. What is the safest next step when implementing fetching data in Next?
Syntax7. Which implementation direction matches the rule for fetching data in Next?
Logic8. Which consequence follows from applying fetching data in Next correctly?
Concept9. Which claim about fetching data in Next is true in this monorepo?
Practical10. Which team practice best demonstrates fetching data in Next?