Explain it simply
Explain it simply
Think of React as bricks: it gives you UI pieces such as buttons, cards and forms. Bricks alone are not a house, though. You still need a building plan: how the rooms connect (routing), where supplies come from (data), and how to have everything ready when a visitor arrives (rendering).
Next.js is that building plan. It does not replace React; it is a framework built on React, so you keep writing components, props and hooks exactly as before. Next.js turns folders into URLs and renders pages on the server ahead of time.
Problem → Why → Solution
The problem: a React app that renders only in the browser
In an app built with React alone (for example with Vite), the first HTML the server sends contains little more than <div id="root"></div>. The browser downloads the JavaScript bundle, runs it, and only then fetches the notes and draws them. On a slow phone connection the user stares at "Loading...", and link-preview bots and some crawlers see a page with no content.
// A browser-only React app: nothing useful exists until this runs
import { useEffect, useState } from "react";
export default function App() {
const [notes, setNotes] = useState(null);
useEffect(() => {
fetch("/api/notes")
.then((res) => res.json())
.then(setNotes);
}, []);
if (!notes) return <p>Loading...</p>;
return <ul>{notes.map((n) => <li key={n.id}>{n.title}</li>)}</ul>;
}Why does this happen?
Both fetching the data and producing the HTML happen in the browser, so you get a chain where each step waits for the previous one: download, run, fetch, render. You also have to pick and wire up a routing library, a data-fetching pattern and a build setup yourself. In Next.js the page is rendered on the server together with its data, and complete HTML is sent, which shortens the whole chain.
Core concept: what React does and what Next.js adds
| Job | Who handles it |
|---|---|
| Components, props, state, hooks and JSX | React |
| Turning folders into URLs (routing) | Next.js |
| Rendering pages on the server or at build time | Next.js (using React's server rendering) |
| Bundling, code splitting, image and font optimisation | Next.js |
React is a library for describing UI with components. Next.js is a framework built on React that adds what a whole website needs: file-system routing, server rendering, patterns for loading data, Route Handlers for API endpoints, Server Actions for forms, and production build tooling. So Next.js is not a new language; what you write is still React code in TSX.
In the App Router every component is a Server Component by default. Server Components run on the server, so they can be async functions and await data from a database or an API directly. Their code is not sent to the browser as JavaScript; only the result is.
When you need useState, useEffect, an event handler like onClick, or a browser API such as window, you write "use client" at the very top of the file to make it a Client Component. Client Components still get their first HTML pre-rendered on the server, and then become interactive in the browser once their JavaScript attaches, a step called hydration.
Next.js also decides whether a page is rendered once at build time (static) or on every request (dynamic), based on the data and APIs the route uses. The caching defaults changed between versions 14, 15 and later releases, so the caching lesson covers the details alongside the docs.
- A request arrives
- Server Components render with data
- Complete HTML goes to the browser
- Client Components hydrate
Send only the small interactive parts to the client; keep everything else on the server.
General pattern: Server and Client Components
// Server Component (default): may be async and read data
export default async function PageName() {
const data = await loadData();
return <main>{/* render data */}</main>;
}
// Client Component: needs state, effects or event handlers
"use client"; // must be the first line of its own file
export function WidgetName() {
/* useState, onClick, browser APIs */
}This pattern shows two files together. A Server Component may be async and await data. "use client" must be the first line of its own file, and it marks that file and everything it imports as client code.
Example: a first Server Component page
This is app/page.tsx for Myanmar Notes. It is an ordinary React component: no hooks, no event handlers and no "use client", so it stays a Server Component. Next.js renders it to HTML on the server and sends that to any browser that opens "/".
export default function HomePage() {
return (
<main>
<h1>Myanmar Notes</h1>
<p>Save notes quickly and easily.</p>
</main>
);
}Code breakdown
- Default export
export default function HomePage() {Next.js uses the component default-exported from app/page.tsx as the "/" route. The function name is up to you, but it must be the default export.
- JSX
return (The JSX is exactly what you learned in React. Next.js needs no new syntax here.
- Heading
<h1>Myanmar Notes</h1>This heading is already inside the HTML the server sends, so users and crawlers see it before any JavaScript runs.
- No client code
<p>Save notes quickly and easily.</p>Nothing on this page is interactive, so no JavaScript for this component needs to reach the browser. To add something like a like button, you would split it into its own Client Component file.
The home route shows the Myanmar Notes heading and an introductory message.Mixing server and client in one page
In practice a page is a Server Component that imports only the small interactive piece as a Client Component. The page below awaits its notes on the server, while LikeButton lives in a client file so it can be clicked in the browser.
// app/page.tsx
import { LikeButton } from "../components/like-button";
async function getNotes() {
return [{ id: 1, title: "Learning routing" }];
}
export default async function HomePage() {
const notes = await getNotes();
return (
<ul>
{notes.map((note) => (
<li key={note.id}>
{note.title} <LikeButton />
</li>
))}
</ul>
);
}
// components/like-button.tsx
"use client";
import { useState } from "react";
export function LikeButton() {
const [likes, setLikes] = useState(0);
return <button onClick={() => setLikes(likes + 1)}>Like ({likes})</button>;
}"use client" marks a boundary
You do not write "use client" in every component. Components imported by a client file automatically become client code too, so the lower in the tree you place the boundary, the less JavaScript goes to the browser.
In real projects
Why this split matters in real projects
Think about a product page in an online shop. A Server Component reads the name, price and description straight from the database and sends them as HTML, so the page appears quickly and search engines can read it. Only the "Add to cart" button and the colour picker are Client Components. Secrets such as the database password stay in server code and never reach the browser.
Blogs, documentation and marketing pages can be generated as static HTML at build time and served instantly from a CDN, while a logged-in user's dashboard is rendered on every request. Being able to mix both kinds in one Next.js app is a major reason teams choose the framework.
Common mistakes
Common mistakes
1. Thinking Next.js is a new language that replaces React
Components, props and useState in Next.js are plain React. What Next.js adds are file conventions (page.tsx, layout.tsx) and components such as next/link and next/image. If your React basics are shaky, revisiting React first is the fastest route.
2. Adding "use client" to every file
Putting "use client" at the top of page.tsx to silence an error moves the whole page and everything it imports into the browser bundle. Client Components cannot be async functions either, so you also lose awaiting data directly on the server. Split out only the interactive piece.
3. Assuming Client Components never run on the server
Client Components are pre-rendered on the server for the first HTML too. Reading window.innerWidth directly in the component body can therefore fail with "window is not defined". Use browser APIs inside useEffect or an event handler.
Best practices
Best practices
- Start every new component as a Server Component and move it to the client only when it needs state or events.
- Keep Client Component files small, and put "use client" on leaf components such as buttons and form inputs.
- When reading docs or blog posts, check that the code targets the App Router and which Next.js version it was written for.
Mini challenge
Mini challenge: a "who does what" page
Write app/about/page.tsx that keeps React's and Next.js's jobs in one typed array and shows them as two lists, grouped by owner.
- The Job type has task: string and owner: "React" | "Next.js"
- Add at least five jobs, covering both owners
- Write a JobList component that takes an owner prop and shows the owner and the count in an <h2>
- Do not use "use client": the page must stay a Server Component
type Job = {
// TODO: task text and an owner limited to two values
};
const jobs: Job[] = [
// TODO: at least five jobs
];
function JobList(/* TODO: owner prop */) {
// TODO: keep only this owner's jobs
// TODO: <section> with <h2>Owner (count)</h2> and a <ul>
}
export default function AboutPage() {
// TODO: heading plus one JobList per owner
}Expected: /about shows the heading "Who does what?", two jobs under "React (2)" and three under "Next.js (3)".
Show hint
Typing the prop as Job["owner"] reuses the union type. jobs.filter((job) => job.owner === owner) gives you one side's jobs.
Show solution
type Job = {
task: string;
owner: "React" | "Next.js";
};
const jobs: Job[] = [
{ task: "Describe UI with components", owner: "React" },
{ task: "Keep state and handle events", owner: "React" },
{ task: "Map folders to URLs", owner: "Next.js" },
{ task: "Render pages on the server", owner: "Next.js" },
{ task: "Optimise images and fonts", owner: "Next.js" },
];
function JobList({ owner }: { owner: Job["owner"] }) {
const mine = jobs.filter((job) => job.owner === owner);
return (
<section>
<h2>
{owner} ({mine.length})
</h2>
<ul>
{mine.map((job) => (
<li key={job.task}>{job.task}</li>
))}
</ul>
</section>
);
}
export default function AboutPage() {
return (
<main>
<h1>Who does what?</h1>
<JobList owner="React" />
<JobList owner="Next.js" />
</main>
);
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- React handles UI components; Next.js is a React framework that adds routing, rendering, data and the build.
- A browser-only app sends nearly empty HTML first, while Next.js pre-renders complete HTML on the server.
- App Router components are Server Components by default, so they can be async and await data directly.
- Split out only the parts that need state, events or browser APIs as Client Components with "use client".
Next.js — Getting Started — Next.js
Next.js — Getting Started — Next.js