AgentSkillsCN

react-component

严格遵循 TypeScript 编程规范,采用容器/呈现器架构与组合优先的设计理念,创建并审查 React 组件。适用于以下场景:(1) 创建 React 组件;(2) 审查 React 组件;(3) 构建 UI 功能;(4) 实现表单;(5) 使用 TanStack Query 添加数据获取功能。贯彻 KISS/YAGNI 原则,运用无头组件模式、Tailwind v4 样式,以及 Web3 安全最佳实践。

SKILL.md
--- frontmatter
name: react-component
description: |
  Create and review React components following strict TypeScript patterns, container/presenter architecture, and composition-first design. Use when asked to: (1) Create a React component, (2) Review a React component, (3) Build UI features, (4) Implement forms, (5) Add data fetching with TanStack Query. Enforces KISS/YAGNI principles, headless component patterns, Tailwind v4 styling, and Web3 security best practices.

React Component Skill

Quick Start

Creating Components

  1. Automated Scaffolding: Use the scaffold-component.mjs script to quickly generate the boilerplate for a new feature component:

    bash
    node .claude/skills/react-component/scripts/scaffold-component.mjs <featureName> <ComponentName>
    
    • <featureName> (camelCase): e.g., userProfile (creates src/features/userProfile/components/).
    • <ComponentName> (PascalCase): e.g., UserProfileCard (generates UserProfileCardContainer.tsx, UserProfileCardView.tsx, UserProfileCardView.stories.tsx). Run this command from the root of your React project.
  2. Read principles.md for core philosophy

  3. Read patterns.md for container/presenter pattern

  4. Read project-structure.md for file placement

Reviewing Components

  1. Read code-review.md for review checklist
  2. Cross-reference with patterns.md and security.md

Component Creation Workflow

Step 1: Determine Component Type

TypePurposeContains
ContainerData fetching, state managementHooks, no complex markup
PresenterPure UI renderingProps, no side effects
HookReusable logicNo JSX

Step 2: Choose Location

code
# Shared UI primitive
src/components/ui/<ComponentName>.tsx

# Feature-specific
src/features/<feature>/components/<Name>Container.tsx
src/features/<feature>/components/<Name>View.tsx

Step 3: Implement Pattern

Container + Presenter pair:

tsx
// Container: handles data
export function FeatureContainer() {
  const { data, error, isLoading } = useFeatureQuery()

  if (isLoading) return <FeatureView state="loading" />
  if (error) return <FeatureView state="error" message={error.message} />
  if (!data) return <FeatureView state="empty" />

  return <FeatureView state="ready" data={data} />
}

// Presenter: handles UI
export function FeatureView({ state, data, message }: FeatureViewProps) {
  // Pure rendering based on props
}

Step 4: Add Storybook

Create stories for all four states: loading, error, empty, ready.

tsx
// FeatureView.stories.tsx
export const Loading = { args: { state: 'loading' } }
export const Empty = { args: { state: 'empty' } }
export const Error = { args: { state: 'error', message: 'Something went wrong' } }
export const Ready = { args: { state: 'ready', data: mockData } }

For more on creating interactive stories with controls, documenting with MDX, and mocking API requests for container components, see the Advanced Storybook Guide.

Step 5: Verify


Key Rules

TypeScript

  • Strict mode required
  • Props interface named <Component>Props
  • Explicit return types on exported functions

Styling

  • Tailwind v4 only (no tailwind.config.js)
  • Use cn() utility for conditional classes
  • Responsive: sm, md, lg, xl minimum

State

  • Remote data: TanStack Query
  • Local UI: useState/useReducer
  • Shared: prop drilling → Context → Zustand (last resort)

Forms

  • React Hook Form + Zod
  • Schema in forms/<schema>.ts
  • Hook in forms/use-<form>-form.ts

Testing

  • Vitest + React Testing Library + MSW
  • Test behavior, not internals
  • No testing Tailwind classes

Reference Files

FileWhen to Read
naming.mdVariable, function, component naming
principles.mdCore philosophy (KISS, YAGNI, UX-first)
patterns.mdContainer/presenter, composition
headless-components.mdHeadless pattern, Radix-style composition
project-structure.mdFile organization
state-and-styling.mdState management, Tailwind, async UX
forms-and-testing.mdRHF + Zod, Vitest + RTL
advanced-storybook.mdInteractive stories, MDX, API mocking
error-handling.mdError boundaries, logging, reporting
security.mdWeb3 safety, logging
accessibility.mda11y best practices, testing
packages.mdApproved dependencies
code-review.mdReview checklist

External Resources