React 生产工程化:可扩展架构指南 - Openclaw Skills
作者:互联网
2026-03-28
什么是 React 生产工程化?
React 生产工程化是一项技术方法论,旨在将 React 开发从简单的组件创建转变为严谨的工程学科。通过将这些 Openclaw Skills 集成到您的 AI 智能体中,您可以获得框架选择决策矩阵、状态管理树和性能预算。该方法论超越了基础的 API 参考,为现代 Web 开发提供可操作的决策框架、模板和评分系统。
其核心价值在于能够标准化跨团队的复杂架构决策。无论您是在 Vite、Next.js 或 Remix 之间做出选择,还是决定在哪里存储服务器状态,这些 Openclaw Skills 都能提供逻辑支持,以构建符合最高行业标准、易于维护且高性能的应用。
下载入口:https://github.com/openclaw/skills/tree/main/skills/1kalin/afrexai-react-production
安装与下载
1. ClawHub CLI
从源直接安装技能的最快方式。
npx clawhub@latest install afrexai-react-production
2. 手动安装
将技能文件夹复制到以下位置之一
全局模式~/.openclaw/skills/
工作区
/skills/
优先级:工作区 > 本地 > 内置
3. 提示词安装
将此提示词复制到 OpenClaw 即可自动安装。
请帮我使用 Clawhub 安装 afrexai-react-production。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。
React 生产工程化 应用场景
- 使用 16 点架构健康检查对现有 React 代码库进行审计。
- 设计基于功能的项目结构,在不产生循环依赖的情况下扩展至数百个路由。
- 通过为特定任务选择正确的工具(Zustand、TanStack Query 或 Context)来实现类型安全的状态管理。
- 在开发生命周期中自动化性能优化和可访问性检查。
- 使用 React Hook Form 和 Zod 设置生产级表单验证。
- AI 智能体初始化架构评估,以确定项目规模、框架和当前的痛点。
- 建立基于功能的结构,隔离功能模块并通过 index 导出定义公共 API。
- 组件开发遵循严格的组件解剖模板,确保 Hook、派生状态和处理函数得到正确组织。
- 应用状态管理决策树,有效分离服务器状态与客户端状态。
- 集成 TypeScript 模式(包括辨别联合类型和品牌类型)以实现最大的类型安全。
- 工作流以生产部署检查表结束,验证性能预算和可访问性合规性。
React 生产工程化 配置指南
要在您的开发环境中使用这些 Openclaw Skills,请确保您的 AI 智能体可以访问方法论文件。使用以下命令初始化配置:
# 安装 React 生产工程化技能包
npx openclaw install afrexai-react-production
# 运行项目健康检查以基准化当前架构
openclaw run check-health --path ./src
React 生产工程化 数据架构与分类体系
该技能根据结构化生命周期组织数据和元数据。这确保了每次 Openclaw Skills 交互都能产生一致的架构输出。
| 数据类别 | 使用格式 | 关键元数据 |
|---|---|---|
| 架构简报 | YAML | 框架、规模、团队人数 |
| 组件设计 | TSX | Props 接口、记忆化策略 |
| 状态映射 | TS | 查询键、Store 选择器 |
| 验证 | Zod | 运行时 Schema、推断类型 |
| 质量评分 | JSON | 性能、测试、a11y 指标 |
name: afrexai-react-production
description: Complete methodology for building production-grade React applications with architecture decisions, component design, state management, performance optimization, testing, and deployment.
React Production Engineering
Complete methodology for building production-grade React applications. Covers architecture decisions, component design, state management, performance optimization, testing, and deployment — not just API reference, but engineering methodology with decision frameworks, templates, and scoring systems.
Phase 1: Architecture Assessment
Quick Health Check (score /16)
- Component tree depth < 6 levels (+2)
- No prop drilling past 2 levels (+2)
- Bundle size < 200KB gzipped (+2)
- LCP < 2.5s on 4G (+2)
- Test coverage > 70% on business logic (+2)
- Zero
anytypes in production code (+2) - No direct DOM manipulation (+2)
- Consistent error boundaries (+2)
Architecture Brief
project:
name: ""
type: "" # spa | ssr | hybrid | static
framework: "" # next | remix | vite-spa | astro
scale: "" # small (<20 routes) | medium (20-100) | large (100+)
team_size: "" # solo | small (2-5) | medium (6-15) | large (15+)
current_state:
react_version: "" # 18 | 19
typescript: true
router: "" # react-router | next-app | tanstack-router
state_management: "" # useState | zustand | jotai | redux | tanstack-query
styling: "" # tailwind | css-modules | styled-components | vanilla-extract
testing: "" # vitest | jest | playwright | cypress
ci_cd: "" # github-actions | gitlab-ci | vercel
pain_points: []
goals: []
Framework Selection Decision Matrix
| Factor | Vite SPA | Next.js | Remix | Astro |
|---|---|---|---|---|
| SEO needed | ? | ? Best | ? Good | ? Best |
| Dashboard/app | ? Best | ? Good | ? Good | ? |
| Content-heavy | ? | ? Good | ? Good | ? Best |
| Team familiarity | ? Simple | ?? Learning curve | ?? Web standards | ?? Islands |
| Deployment | Anywhere | Vercel optimal | Anywhere | Anywhere |
| Bundle size | You control | Framework overhead | Smaller | Minimal JS |
Decision rules:
- Dashboard/internal tool with no SEO → Vite SPA
- Marketing + app hybrid → Next.js
- Content-first with some interactivity → Astro
- Web-standards-first, nested layouts → Remix
- Default for most SaaS products → Next.js
Phase 2: Project Structure & Conventions
Recommended Feature-Based Structure
src/
├── app/ # Routes/pages (framework-specific)
├── features/ # Feature modules (THE core pattern)
│ ├── auth/
│ │ ├── components/ # Feature-specific components
│ │ ├── hooks/ # Feature-specific hooks
│ │ ├── api/ # API calls & types
│ │ ├── utils/ # Feature utilities
│ │ ├── types.ts # Feature types
│ │ └── index.ts # Public API (barrel export)
│ ├── dashboard/
│ └── settings/
├── shared/ # Cross-feature shared code
│ ├── components/ # Generic UI components
│ │ ├── ui/ # Primitives (Button, Input, Card)
│ │ └── layout/ # Layout components
│ ├── hooks/ # Generic hooks
│ ├── lib/ # Utilities, constants
│ └── types/ # Global types
├── providers/ # Context providers
└── styles/ # Global styles
7 Structure Rules
- Feature isolation — features/ never import from other features directly; use shared/ or events
- Barrel exports — every feature has index.ts that defines its public API
- Colocation — tests, stories, and styles live next to their component
- Max file size — 300 lines. If bigger, split
- Max component size — 50 lines of JSX. If bigger, extract
- No circular deps — enforce with eslint-plugin-import
- Types colocated — feature types in feature, shared types in shared/types
Naming Conventions
Components: PascalCase.tsx (UserProfile.tsx)
Hooks: useCamelCase.ts (useAuth.ts)
Utilities: camelCase.ts (formatCurrency.ts)
Types: PascalCase.ts (User.ts) or types.ts
Constants: SCREAMING_SNAKE.ts (API_ENDPOINTS.ts)
Test files: *.test.tsx (UserProfile.test.tsx)
Story files: *.stories.tsx (Button.stories.tsx)
Phase 3: Component Design Patterns
Component Anatomy Template
// 1. Imports (grouped: react → third-party → internal → types → styles)
import { useState, useCallback, memo } from 'react'
import { clsx } from 'clsx'
import { Button } from '@/shared/components/ui'
import type { User } from '../types'
// 2. Types (exported for reuse)
export interface UserCardProps {
user: User
onEdit?: (id: string) => void
variant?: 'compact' | 'full'
className?: string
}
// 3. Component (named export, not default)
export const UserCard = memo(function UserCard({
user,
onEdit,
variant = 'full',
className,
}: UserCardProps) {
// 4. Hooks first
const [isExpanded, setIsExpanded] = useState(false)
// 5. Derived state (no useEffect for derived!)
const displayName = `${user.firstName} ${user.lastName}`
// 6. Handlers (useCallback for passed-down refs)
const handleEdit = useCallback(() => {
onEdit?.(user.id)
}, [onEdit, user.id])
// 7. Early returns for edge cases
if (!user) return null
// 8. JSX (max 50 lines)
return (
{displayName}
{variant === 'full' && {user.bio}
}
{onEdit && }
)
})
Component Composition Patterns
1. Compound Components (for related UI groups)
// Usage: A ...
const TabsContext = createContext(null)
export function Tabs({ children, defaultValue }: TabsProps) {
const [activeTab, setActiveTab] = useState(defaultValue)
return (
{children}
)
}
Tabs.List = TabsList
Tabs.Tab = TabsTab
Tabs.Panel = TabsPanel
2. Render Props (for flexible rendering logic)
export function DataList({ items, renderItem, renderEmpty }: DataListProps) {
if (items.length === 0) return renderEmpty?.() ??
return {items.map((item, i) => - {renderItem(item)}
)}
}
3. Higher-Order Components (for cross-cutting concerns — use sparingly)
export function withAuth(Component: ComponentType
) {
return function AuthenticatedComponent(props: P) {
const { user, isLoading } = useAuth()
if (isLoading) return
if (!user) return
return
}
}
10 Component Rules
- One component per file — always
- Named exports — never default exports (refactoring safety)
- Props interface — always explicit, always exported
- No business logic in components — extract to hooks
- No inline styles — use Tailwind classes or CSS modules
- No string refs — useRef only
- No index as key — use stable identifiers
- Memo strategically — not everywhere, only for expensive renders
- Children over props — prefer composition over configuration
- Accessible by default — semantic HTML, ARIA when needed
Phase 4: State Management Decision Framework
State Type Decision Tree
Is it server data (from API)?
├─ YES → TanStack Query (or SWR) — NEVER Redux/Zustand for server state
│
└─ NO → Is it shared across features?
├─ YES → Is it complex with many actions?
│ ├─ YES → Zustand (or Redux Toolkit if team knows it)
│ └─ NO → Jotai (atomic) or Zustand (simple store)
│
└─ NO → Is it shared within a feature?
├─ YES → Context + useReducer (or Zustand feature store)
└─ NO → useState / useReducer (component-local)
State Management Comparison
| Tool | Best For | Bundle | Learning | Team Size |
|---|---|---|---|---|
| useState | Component-local | 0 KB | None | Any |
| useReducer | Complex local state | 0 KB | Low | Any |
| Context | Feature-scoped, low-frequency | 0 KB | Low | Any |
| Zustand | Global client state | 1.1 KB | Low | Any |
| Jotai | Atomic derived state | 3.4 KB | Medium | Small-Med |
| TanStack Query | Server state | 12 KB | Medium | Any |
| Redux Toolkit | Complex global + middleware | 11 KB | High | Large |
Server State with TanStack Query
// api/users.ts — query key factory pattern
export const userKeys = {
all: ['users'] as const,
lists: () => [...userKeys.all, 'list'] as const,
list: (filters: Filters) => [...userKeys.lists(), filters] as const,
details: () => [...userKeys.all, 'detail'] as const,
detail: (id: string) => [...userKeys.details(), id] as const,
}
// hooks/useUsers.ts
export function useUsers(filters: Filters) {
return useQuery({
queryKey: userKeys.list(filters),
queryFn: () => fetchUsers(filters),
staleTime: 5 * 60 * 1000, // 5 min
placeholderData: keepPreviousData,
})
}
export function useUpdateUser() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: updateUser,
onMutate: async (newUser) => {
// Optimistic update
await queryClient.cancelQueries({ queryKey: userKeys.detail(newUser.id) })
const previous = queryClient.getQueryData(userKeys.detail(newUser.id))
queryClient.setQueryData(userKeys.detail(newUser.id), newUser)
return { previous }
},
onError: (err, newUser, context) => {
queryClient.setQueryData(userKeys.detail(newUser.id), context?.previous)
},
onSettled: (data, err, variables) => {
queryClient.invalidateQueries({ queryKey: userKeys.detail(variables.id) })
queryClient.invalidateQueries({ queryKey: userKeys.lists() })
},
})
}
Client State with Zustand
// stores/useUIStore.ts — thin, focused stores
interface UIStore {
sidebarOpen: boolean
theme: 'light' | 'dark' | 'system'
toggleSidebar: () => void
setTheme: (theme: UIStore['theme']) => void
}
export const useUIStore = create()(
persist(
(set) => ({
sidebarOpen: true,
theme: 'system',
toggleSidebar: () => set((s) => ({ sidebarOpen: !s.sidebarOpen })),
setTheme: (theme) => set({ theme }),
}),
{ name: 'ui-preferences' }
)
)
// Usage: const theme = useUIStore((s) => s.theme) — always use selectors!
5 State Management Rules
- Server state ≠ client state — never mix them in the same store
- Smallest scope possible — useState > Context > Zustand > Redux
- No useEffect for derived state — use useMemo or compute inline
- Selectors always —
useStore(s => s.field)notuseStore() - URL is state — search params, filters, pagination → URL, not React state
Phase 5: Hooks Engineering
Custom Hook Template
// hooks/useDebounce.ts
export function useDebounce(value: T, delayMs: number = 300): T {
const [debouncedValue, setDebouncedValue] = useState(value)
useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delayMs)
return () => clearTimeout(timer)
}, [value, delayMs])
return debouncedValue
}
Essential Custom Hooks Library
| Hook | Purpose | When to Use |
|---|---|---|
useDebounce |
Debounce value changes | Search inputs, resize |
useMediaQuery |
Responsive breakpoints | Conditional rendering |
useLocalStorage |
Persistent local state | Preferences, drafts |
useIntersection |
Viewport detection | Lazy load, infinite scroll |
usePrevious |
Track previous value | Animations, comparisons |
useClickOutside |
Detect outside clicks | Dropdowns, modals |
useEventListener |
Safe event binding | Keyboard, scroll, resize |
useToggle |
Boolean state toggle | Modals, accordions |
Hook Rules (beyond React's rules)
- One concern per hook —
useUserSearchnotuseEverything - Return tuple or object — tuple for 1-2 values, object for 3+
- Accept options object —
useDebounce(value, { delay: 300 })scales better - Handle cleanup — every subscription/timer needs cleanup in useEffect return
- No hooks in conditions — extract conditional logic into the hook body
- Test hooks independently — use
renderHookfrom testing-library
Phase 6: TypeScript Integration
Strict Configuration
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"@/*": ["./src/*"]
}
}
}
Essential Type Patterns
// 1. Discriminated unions for state machines
type AsyncState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: T }
| { status: 'error'; error: Error }
// 2. Polymorphic components
type ButtonProps = {
as?: C
variant?: 'primary' | 'secondary'
} & ComponentPropsWithoutRef
export function Button({
as,
variant = 'primary',
...props
}: ButtonProps) {
const Component = as || 'button'
return
}
// 3. Branded types for IDs
type UserId = string & { __brand: 'UserId' }
type PostId = string & { __brand: 'PostId' }
// 4. Zod for runtime validation
const userSchema = z.object({
id: z.string().uuid(),
email: z.string().email(),
role: z.enum(['admin', 'user', 'viewer']),
})
type User = z.infer
5 TypeScript Rules
- Zero
any— useunknownand narrow, or generics - Zod at boundaries — validate all external data (API, forms, URL params)
- Discriminated unions over optional fields —
{ status: 'success'; data: T }not{ data?: T; error?: Error } - Branded types for IDs — prevent
userIdbeing passed wherepostIdexpected - Satisfies over as —
config satisfies Configpreserves inference;as Configlies
Phase 7: Performance Optimization
Performance Budget
| Metric | Target | Measurement |
|---|---|---|
| First Contentful Paint | < 1.8s | Lighthouse |
| Largest Contentful Paint | < 2.5s | Lighthouse |
| Interaction to Next Paint | < 200ms | Lighthouse |
| Cumulative Layout Shift | < 0.1 | Lighthouse |
| Bundle size (gzipped) | < 200 KB | webpack-bundle-analyzer |
| JS execution (main thread) | < 3s | Chrome DevTools |
Optimization Priority Stack
| Priority | Technique | Impact | Effort |
|---|---|---|---|
| P0 | Code splitting (route-based) | ?? High | Low |
| P0 | Image optimization (next/image, srcset) | ?? High | Low |
| P1 | Tree shaking (named imports) | ?? Medium | Low |
| P1 | Virtualization for long lists | ?? Medium | Medium |
| P1 | Debounce expensive operations | ?? Medium | Low |
| P2 | React.memo on expensive components | ?? Low-Med | Low |
| P2 | useMemo/useCallback for expensive calculations | ?? Low-Med | Low |
| P3 | Web Workers for heavy computation | ?? Low | High |
Code Splitting Patterns
// 1. Route-based (automatic with Next.js, manual with React Router)
const Dashboard = lazy(() => import('./features/dashboard'))
const Settings = lazy(() => import('./features/settings'))
// 2. Component-based (heavy components)
const Chart = lazy(() => import('./components/Chart'))
const MarkdownEditor = lazy(() =>
import('./components/MarkdownEditor').then(m => ({ default: m.MarkdownEditor }))
)
// 3. Library-based (heavy third-party)
const { PDFViewer } = await import('@react-pdf/renderer')
React Compiler (React 19+)
// With React Compiler enabled, manual memo/useMemo/useCallback become unnecessary
// The compiler auto-memoizes. Remove manual optimizations:
// ? const memoized = useMemo(() => expensiveCalc(data), [data])
// ? const memoized = expensiveCalc(data) // compiler handles it
// Enable in babel config:
// plugins: [['babel-plugin-react-compiler', {}]]
Rendering Performance Rules
- Never create components inside components — define at module level
- Never create objects/arrays in JSX —
style={{ color: 'red' }}rerenders always - Children as props prevent rerender —
- Key must be stable and unique — not index, not
Math.random() - Avoid context value churn — memoize provider value or split contexts
- Profile before optimizing — React DevTools Profiler, not guesswork
Phase 8: Error Handling & Resilience
Error Boundary Architecture
// Three levels of error boundaries:
// 1. App-level (catches everything, shows full-page error)
// 2. Feature-level (isolates feature failures)
// 3. Component-level (for risky widgets — charts, third-party)
// Modern error boundary with react-error-boundary
import { ErrorBoundary, FallbackProps } from 'react-error-boundary'
function FeatureErrorFallback({ error, resetErrorBoundary }: FallbackProps) {
return (
Something went wrong
{error.message}
)
}
// Usage:
queryClient.clear()}>
Error Handling Checklist
- App-level error boundary wrapping entire app
- Feature-level boundaries for each major feature
- API errors handled in TanStack Query's
onError/ error states - Form validation errors shown inline (not alerts)
- 404 page for unknown routes
- Offline detection and graceful degradation
- Error reporting to monitoring (Sentry, etc.)
- User-friendly error messages (no stack traces in production)
Phase 9: Forms & Validation
Form Library Decision
| Library | Best For | Bundle | Renders |
|---|---|---|---|
| React Hook Form | Most forms | 9 KB | Minimal (uncontrolled) |
| Formik | Simple forms | 13 KB | Every keystroke |
| TanStack Form | Type-safe complex | 5 KB | Controlled |
| Native | 1-2 field forms | 0 KB | You control |
Default recommendation: React Hook Form + Zod
Form Pattern
const schema = z.object({
email: z.string().email('Invalid email'),
password: z.string().min(8, 'Min 8 characters'),
role: z.enum(['admin', 'user']),
})
type FormData = z.infer
export function LoginForm({ onSubmit }: { onSubmit: (data: FormData) => void }) {
const form = useForm({
resolver: zodResolver(schema),
defaultValues: { email: '', password: '', role: 'user' },
})
return (
)
}
Phase 10: Testing Strategy
Test Pyramid for React
| Level | Tool | Coverage Target | What to Test |
|---|---|---|---|
| Unit | Vitest | 80% business logic | Hooks, utilities, reducers |
| Component | Testing Library | Key user flows | Rendering, interactions, a11y |
| Integration | Testing Library | Feature flows | Multi-component workflows |
| E2E | Playwright | Critical paths | Auth, checkout, core flows |
| Visual | Chromatic/Percy | UI components | Regression detection |
Testing Patterns
// Component test (Testing Library philosophy: test behavior, not implementation)
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
describe('UserCard', () => {
it('calls onEdit when edit button clicked', async () => {
const user = userEvent.setup()
const onEdit = vi.fn()
render( )
await user.click(screen.getByRole('button', { name: /edit/i }))
expect(onEdit).toHaveBeenCalledWith(mockUser.id)
})
it('does not render edit button when onEdit not provided', () => {
render( )
expect(screen.queryByRole('button', { name: /edit/i })).not.toBeInTheDocument()
})
})
7 Testing Rules
- Test behavior, not implementation — never test state directly or useEffect
- Use accessible queries —
getByRole>getByTestId>getByText - User events over fireEvent —
userEvent.clicksimulates real interaction - One assertion per concept — not one per test, but focused assertions
- Mock at boundaries — API calls, not internal functions
- No snapshot tests — they break on every change and test nothing meaningful
- Arrange-Act-Assert — clear structure in every test
Phase 11: Accessibility (a11y)
10-Point Accessibility Checklist
- Semantic HTML —
not相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
信号管道:自动化营销情报工具 - Openclaw Skills
技能收益追踪器:监控 Openclaw 技能并实现变现
AI 合规准备就绪度:评估与治理工具 - Openclaw Skills
FOSMVVM ServerRequest 测试生成器:自动化 API 测试 - Openclaw Skills
酒店搜索器:AI 赋能的住宿与位置情报 - Openclaw Skills
Dub 链接 API:程序化链接管理 - Openclaw Skills
IntercomSwap:P2P BTC 与 USDT 跨链兑换 - Openclaw Skills
spotplay:macOS 原生 Spotify 播放控制 - Openclaw Skills
DeepSeek OCR:AI驱动的图像文本识别 - Openclaw Skills
Web Navigator:自动化网页研究与浏览 - Openclaw Skills
AI精选
