搜索

PythonReactSEOPDF
GitHub Skill

nextjs-server-components

RSC usage, "use client" directive, and Component Purity.

HoangNguyen0403☆ 143 仓库 Stars48 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Server & Client Components

Priority: P0 (CRITICAL)

Next.js (App Router) uses React Server Components (RSC) by default.

Server Components (Default)

  • Behavior: Rendered on server, sent as HTML/Payload to client. Zero bundle size for included libs.
  • Capabilities: Async/Await, Direct DB access, Secrets usage.
  • Restrictions: No useState, useEffect, or Browser APIs (window, localstorage).

Client Components

  • Directive: Add 'use client' at the VERY TOP of the file.
  • Usage: Interactivity (onClick), State (useState), Lifecycle effects, Browser APIs.
  • Strategy: Move Client Components to the leaves of the tree.
    • Bad: Making the root layout a Client Component.
    • Good: Wrapping a <Button /> in a Client Component.

Composition Patterns

  • Server-in-Client: You cannot import a Server Component directly into a Client Component.
    • Fix: Pass Server Component as children prop to the Client Component.
// ClientWrapper.tsx
'use client';
export default function ClientWrapper({ children }) {
  return <div>{children}</div>;
}

// Page.tsx (Server)
<ClientWrapper>
  <ServerComponent />
</ClientWrapper>;

Anti-Patterns

  • Poisoning: Importing server-only secrets into Client Components (Use server-only package to prevent).
  • Over-fetching: Passing large data props to Client Components (Serialization cost). Only pass IDs if possible.

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!