← 返回
开发者工具 中文

Building Components

React component building and composition best practices. Use when creating, reviewing, or refactoring React components. Covers component structure, props pat...
React 组件构建与组合最佳实践,适用于创建、审查或重构 React 组件,涵盖组件结构、props 模式、状态管理、hooks、性能、测试与文档。
eaveluo
开发者工具 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 583
下载
💾 10
安装
1
版本
#latest

概述

Building React Components

Best practices for building reusable, maintainable React components.

When to Apply

Reference these guidelines when:

  • Creating new React components
  • Reviewing component structure and API design
  • Refactoring components for better reusability
  • Implementing component composition patterns
  • Designing props interfaces

Core Principles

1. Single Responsibility

Each component should do one thing well. Split large components into smaller, focused pieces.

2. Composition Over Inheritance

Prefer composing components together rather than complex inheritance hierarchies.

// ✅ Good: Composition
function Page() {
  return (
    <Layout>
      <Header />
      <Main>
        <Article />
      </Main>
      <Footer />
    </Layout>
  );
}

// ❌ Avoid: Deep nesting
function Page() {
  return <LayoutWithHeaderAndFooter><MainContent /></LayoutWithHeaderAndFooter>;
}

3. Props Design

  • Use TypeScript for props typing
  • Keep props interfaces simple and focused
  • Prefer many small props over few large objects
  • Use children prop for content composition

4. Component Structure

// ✅ Recommended structure
import { FC } from 'react';

interface Props {
  title: string;
  children?: React.ReactNode;
}

export const Card: FC<Props> = ({ title, children }) => {
  return (
    <div className="card">
      <h2>{title}</h2>
      {children}
    </div>
  );
};

5. State Management

  • Keep state as close to where it's used as possible
  • Lift state up only when necessary
  • Consider custom hooks for reusable state logic

Common Patterns

Compound Components

For flexible APIs like Select/Option, Tabs/TabList/Tab/TabPanel.

Render Props

For sharing behavior while keeping rendering control.

Hooks

For sharing stateful logic across components.

Related Skills

  • vercel-react-best-practices
  • next-best-practices
  • vercel-composition-patterns

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 11:36 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 668 📥 324,132
ai-intelligence

Stock Monitor Pro

eaveluo
专业级智能股票监控预警系统 V2.1。支持收盘日报自动生成、反爬虫优化(Session级UA、多数据源冗余)、成本百分比预警、均线金叉死叉、RSI超买超卖、成交量异动监控、智能错误提醒。符合中国投资者习惯(红涨绿跌)。Use when us
★ 6 📥 2,801
developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 68 📥 180,146