Skip to content

下一个.js 基础

服务器-边渲染, App Router, 数据获取, and 为什么下一个.js 是 React meta-框架.

你将学到

  • 什么下一个.js 是 and 如何它 extends React
  • 服务器-边渲染 and 静态 generation
  • App Router and file-based routing
  • 服务器 and 客户端组件

概念讲解

What Is Next.js?

Next.js is a React meta-framework built by Vercel. It extends React with server-side rendering (SSR), static site generation (SSG), file-based routing, API routes, image optimization, and more. While plain React is a client-side library, Next.js gives you a full-stack framework with sensible defaults. It is the recommended way to start new React projects (the React team itself recommends Next.js for new apps). Next.js handles the build tooling, routing, and rendering strategy so you can focus on building features.

Rendering Strategies

Next.js supports multiple rendering strategies per page. Static Site Generation (SSG) pre-renders pages at build time — the fastest option, ideal for blogs and marketing pages. Server-Side Rendering (SSR) renders on the server on each request — ideal for personalized, frequently changing content. Incremental Static Regeneration (ISR) regenerates static pages in the background at a specified interval — a hybrid approach. In the App Router (Next.js 13+), components are server components by default, rendering on the server with zero client JavaScript.

The App Router

Next.js 13 introduced the App Router, which uses the app/ directory for file-based routing. Each folder is a route segment, and page.tsx defines the UI for that route. 布局.tsx defines shared layouts that wrap child pages. 加载.tsx shows a fallback during navigation. 错误.tsx handles errors. This file convention eliminates the need for a separate routing configuration — the file system IS the router. Nested layouts and parallel routes are powerful features of the App Router.

Server and Client Components

In the App Router, components are server components by default — they render on the server, cannot use hooks or browser APIs, and send zero JavaScript to the client. To make a component a client component, add the "use 客户端" directive at the top. Client components can use hooks, event handlers, and browser APIs. This split lets you ship less JavaScript: most of your page can be server components, with small interactive islands as client components. This is a fundamental shift from traditional React where everything runs in the browser.

示例代码

              // app/page.tsx — Server Component (default in App Router).
// Runs on the server, can fetch data directly, sends zero JS to client.
async function HomePage() {
  // Direct data fetching on the server — no useEffect needed.
  const res = await fetch("https://api.example.com/posts", {
    next: { revalidate: 60 }, // ISR: revalidate every 60 seconds.
  });
  const posts = await res.json();

  return (
    <main>
      <h1>Latest Posts</h1>
      {posts.map(post => (
        <article key={post.id}>
          <h2>{post.title}</h2>
          <p>{post.excerpt}</p>
          <LikeButton postId={post.id} /> {/* Client component island */}
        </article>
      ))}
    </main>
  );
}

// app/components/LikeButton.tsx — Client Component.
"use client"; // This directive makes it a client component.
import { useState } from "react";

export function LikeButton({ postId }: { postId: number }) {
  const [liked, setLiked] = useState(false);
  return (
    <button onClick={() => setLiked(!liked)}>
      {liked ? "♥ Liked" : "♡ Like"}
    </button>
  );
}
            

HomePage 是服务器组件那个获取数据 directly on 服务器. LikeButton 是客户端组件 (use 客户端) 那个需要 interactivity. 仅 LikeButton ships JavaScript 到浏览器.

动手试一试

常见错误

错误写法

添加 'use 客户端' 到每个组件通过默认

正确做法

服务器组件 (默认) ship 零 JavaScript and 能获取数据 directly. 仅添加 'use 客户端' 到组件那个需要 Hook, 事件处理函数, or 浏览器 APIs. Keeping 大多数组件 as 服务器组件 reduces bundle 大小.

错误写法

Using useEffect for 数据获取 in 下一个.js 服务器组件

正确做法

服务器组件能 获取数据 directly 使用异步/等待 — 否 useEffect 需要. useEffect 是 for 客户端-边边 副作用. 获取 in useEffect causes 加载 flash and waterfalls 请求.

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误