All posts

6 min read

React developer learning Next.js: what carries over, what's new, 30-day plan

Practical guide for React devs to transition to Next.js, covering shared concepts, new features, pitfalls, and a 30‑day learning plan.

What carries over

React conceptHow it works in Next.jsWhy it still matters
JSXJSX files (.tsx or .jsx) are compiled by the same Babel pipeline.All component rendering logic stays identical.
Function components & hooksuseState, useEffect, useContext, custom hooks, etc., are imported from react exactly the same way.State management and side‑effects remain component‑local.
Component compositionYou still compose UI by nesting components, passing props, and using children.The mental model of “building UI from small pieces” does not change.
React Router vs. Next.js routingThe <Link> component exists in both ecosystems, but Next.js uses file‑system routing.Navigation still uses <Link href="/about">.
CSS‑in‑JS / CSS Modulesstyled-components, emotion, or CSS Modules (.module.css) work unchanged.Styling strategies you already know continue to apply.
Testing with Jest / React Testing LibraryTest files live alongside components; the same APIs (render, screen, fireEvent) are used.Unit and integration tests remain the same, only the import paths may differ.
TypeScript supporttsconfig.json is still the source of truth; you can use tsx files everywhere.Type safety carries over without extra configuration.
Environment variablesprocess.env.NEXT_PUBLIC_* works like any process.env you already use.No need to learn a new way to read env vars; just the naming convention.
SSR concepts (hydration)You already know that the server renders HTML and the client “hydrates” it. Next.js just automates the server side.Your mental model of hydration is already correct.

What's new

Next.js adds a layer of conventions and built‑in features that React alone does not provide. Below is a side‑by‑side comparison of a common task: fetching data for a page.

Pure React (client‑side fetch)

// src/pages/UserList.jsx
import { useEffect, useState } from "react";

export default function UserList() {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    fetch("/api/users")
      .then((res) => res.json())
      .then(setUsers);
  }, []);

  return (
    <ul>
      {users.map((u) => (
        <li key={u.id}>{u.name}</li>
      ))}
    </ul>
  );
}

Next.js (static generation with getStaticProps)

// pages/users.tsx
import { GetStaticProps, NextPage } from "next";

type User = { id: number; name: string };
type Props = { users: User[] };

export const getStaticProps: GetStaticProps<Props> = async () => {
  const res = await fetch("https://example.com/api/users");
  const users = await res.json();
  return { props: { users } };
};

const UsersPage: NextPage<Props> = ({ users }) => (
  <ul>
    {users.map((u) => (
      <li key={u.id}>{u.name}</li>
    ))}
  </ul>
);

export default UsersPage;

What changed and why

  1. File‑system routing – In Next.js the file pages/users.tsx automatically becomes the /users route. No explicit router configuration is needed.
  2. Data‑fetching primitives – getStaticProps runs at build time (or on-demand with ISR). The component receives data as props, eliminating the need for a client‑side useEffect for the initial load.
  3. Type safety – GetStaticProps and NextPage are exported from next, giving you typed props out of the box.
  4. Performance – The HTML is generated ahead of time, so the first paint is fully rendered without a loading spinner.

Other notable new concepts:

  • pages/api – Serverless functions live next to your UI code.
  • Image optimization – <Image> from next/image automatically serves responsive, WebP‑enabled images.
  • Middleware – Edge‑runtime code in middleware.ts can run before a request reaches a page.
  • App Router (optional) – The newer app/ directory introduces React Server Components, but the classic pages/ router remains fully supported.

Common pitfalls

PitfallSymptomFix
Using window or document at the top level“ReferenceError: window is not defined” during build or SSRGuard the code (if (typeof window !== "undefined")) or move it inside useEffect.
Forgetting the public/ prefix for static assetsImages load locally in dev but 404 in productionPlace assets in public/ and reference them as /my‑image.png.
Mixing getStaticProps with useEffect for the same dataDuplicate network requests, stale UIChoose one source of truth: either fetch at build time (getStaticProps) or client side (useEffect).
Incorrect Link usageFull page reload instead of client navigationUse <Link href="/about"><a>About</a></Link> (or the newer <Link href="/about">About</Link> in Next 13+).
Over‑using getServerSidePropsSlow page loads because every request hits the serverPrefer getStaticProps with Incremental Static Regeneration (revalidate) when possible.
Neglecting the NEXT_PUBLIC_ prefixEnv var is undefined on the clientPrefix any variable you need in the browser with NEXT_PUBLIC_.
Improper file naming in pages/Route does not match expected URL (e.g., Home.jsx vs index.jsx)The default route is pages/index.{js,tsx,jsx}; other routes map directly to file names.
Using next/link with external URLsLink triggers client navigation to an external site, breaking the SPA modelUse a regular <a href="https://example.com"> for external links or add target="_blank".

Your 30‑day plan

Week 1 – Foundations

  1. Create a fresh Next.js project
    npx create-next-app@latest my-next-app
    cd my-next-app
    npm run dev
    
  2. Explore the file‑system router – Add pages/about.jsx, pages/blog/[slug].jsx, and observe URL mapping.
  3. Convert an existing React component (e.g., a button library) into a Next.js component; keep the same props and styling.
  4. Mini project: Build a static “Portfolio” site with Home, About, and Contact pages, using only pages/ routing and CSS Modules.

Week 2 – Data fetching & API routes

  1. Learn getStaticProps and getStaticPaths – Fetch a list of posts from a public API and generate a detail page for each.
  2. Introduce getServerSideProps – Create a dashboard that shows user‑specific data based on a cookie.
  3. Create an API route in pages/api/hello.ts that returns JSON; call it from the client side with fetch.
  4. Mini project: Build a “News Reader” that statically generates article list pages and uses an API route for a comment submission form.

Week 3 – Performance & advanced features

  1. Use <Image> – Replace <img> tags with next/image and experiment with layout="responsive" and placeholder="blur".
  2. Implement Incremental Static Regeneration – Add revalidate: 60 to a getStaticProps page and observe updates without a rebuild.
  3. Add middleware – Write a simple middleware.ts that redirects unauthenticated users to /login.
  4. Mini project: Create a “Product Catalog” with static product pages, ISR for price updates, and a protected admin route guarded by middleware.

Week 4 – Full‑stack polish & testing

  1. Integrate TypeScript (if not already) – Rename files to .tsx, add tsconfig.json, and fix any type errors.
  2. Write unit and integration tests using Jest and React Testing Library; test a page that uses getStaticProps.
  3. Deploy to Vercel (or another platform) – Connect the repo, run a production build, and verify that ISR works in the live environment.
  4. Mini project: Refactor the “Product Catalog” into a production‑ready app with CI, tests, and a live deployment.

FAQ

How do I decide between getStaticProps and getServerSideProps?

Use getStaticProps for data that changes infrequently or can be regenerated on a schedule; choose getServerSideProps when you need fresh data on every request (e.g., auth‑dependent pages).

Can I still use React Router inside a Next.js app?

You can, but it’s unnecessary for page navigation because Next.js handles routing via the pages/ (or app/) directory. Use React Router only for deeply nested UI state that isn’t reflected in the URL.

Do I need to install React separately?

No. Next.js bundles the appropriate version of React, so you import from "react" as usual.

How does CSS Modules naming differ from plain CSS?

A file named Button.module.css is imported as an object (import styles from "./Button.module.css"), and class names are scoped automatically (className={styles.primary}). This prevents global leakage without extra configuration.

Keep going

#react #nextjs #learning #guide