首页/ AI 技能 / @skilllib/skill-2117939930272415867
Skill站外收录 @skilllib/skill-2117939930272415867

server-components

提供 Next.js App Router 中服务端与客户端组件的协同开发能力,支持服务端直出 HTML、数据库访问及密钥安全使用,同时允许在必要位置启用交互逻辑与浏览器 API,强调组件纯度、合理分层和数据传递优化。

0.0 (0) 37 次安装 原作者 编程导航
收录于 2026-01-22
软件开发 AI 技能

Skills.MD

# 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. ```tsx // 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.

评论 0

还没有评论,来抢沙发。