← 返回
未分类 中文

ia-react-frontend

React architecture patterns, TypeScript, Next.js, hooks, and testing. Use when working with React component structure, state management, Next.js routing, Vit...
React 架构模式、TypeScript、Next.js、Hooks 与测试,适用于 React组件结构、状态管理、Next.js 路由及 Vitest 等场景。
iliaal iliaal 来源
未分类 clawhub v4.1.0 2 版本 99898.7 Key: 无需
★ 0
Stars
📥 986
下载
💾 0
安装
2
版本
#latest

概述

React Frontend

Verify before implementing: For App Router patterns, React 19 APIs, or version-specific behavior, look up current docs via Context7 (query-docs) before writing code. Training data may lag current releases.

Component TypeScript

  • Extend native elements with ComponentPropsWithoutRef<'button'>, add custom props via intersection
  • Use React.ReactNode for children, React.ReactElement for single element, render prop (data: T) => ReactNode
  • Discriminated unions for variant props -- TypeScript narrows automatically in branches
  • Generic components: with keyof T for column keys, T extends { id: string } for constraints
  • Event types: React.MouseEvent, FormEvent, ChangeEvent
  • as const for custom hook tuple returns
  • useRef(null) for DOM (use ?.), useRef(0) for mutable values
  • Explicit useState(null) for unions/null
  • useReducer actions as discriminated unions: { type: 'set'; payload: number } | { type: 'reset' }
  • useContext null guard: throw in custom useX() hook if context is null

Effects Decision Tree

Effects are escape hatches -- most logic should NOT use effects.

NeedSolution
----------------
Derived value from props/stateCalculate during render (useMemo if expensive)
Reset state on prop changekey prop on component
Respond to user eventEvent handler
Notify parent of state changeCall onChange in event handler, or fully controlled component
Chain of state updatesCalculate all next state in one event handler
Sync with external systemEffect with cleanup

Effect rules:

  • Never suppress the linter -- fix the code instead
  • Use updater functions (setItems(prev => [...prev, item])) to remove state dependencies
  • Move objects/functions inside effects to stabilize dependencies
  • useEffectEvent for non-reactive values (e.g., theme in a connection effect)
  • Always return cleanup for subscriptions, connections, listeners
  • Data fetching cancellation (pick by situation): AbortController for fetch; ignore flag for non-cancellable promises; React Query handles both automatically

Concurrency & Race Classes

Frontend bugs that survive type-checking and unit tests usually land in one of five race classes. Hunt each one explicitly during review:

  1. Lifecycle cleanup gaps -- in-production signal: "Can't perform state update on an unmounted component" warnings, slow-burn memory leaks under rapid navigation, duplicate event handlers firing. Root cause: useEffect registered a listener/timer/observer without returning cleanup (see Effect rules above for the rule).
  2. Remount-timing mistakes -- async callbacks mutate DOM or state after swap / disconnect / route change. Classic cases: a fetch().then(setData) resolves after navigation to a different route; a requestAnimationFrame fires after the parent unmounts. See "Data fetching" in Effect rules above for the cancellation hierarchy.
  3. Boolean-as-state for UI that isn't binary -- isLoading: boolean can't represent idle | loading | success | error | retry without creating inconsistent combinations (isLoading: true, error: Error is contradictory). Prefer an explicit state constant ('idle' | 'loading' | 'success' | 'error') with a transition function so invalid states are unreachable.
  4. Stale promises and timers with no cancel path -- a promise chain or setTimeout holds a reference to setState after the component's moved on. Bind every async operation to a cancel mechanism per the cancellation hierarchy above, and verify the cleanup path is exercised by a test.
  5. Per-element handlers where delegation would be safer -- attaching onClick to every row in a list creates N closures and N subscriptions; delegated listeners (single handler on the parent reading event.target.closest(...)) are safer under rapid re-renders, avoid stale-closure bugs, and scale to large lists. Use delegation when the list exceeds ~50 items or updates frequently.

These classes produce bugs that are intermittent, environment-dependent, and invisible to type-checking -- exactly the ones that reach production. Review for them deliberately, not just as "subscriptions need cleanup."

State Management

Local UI state       → useState, useReducer
Shared client state  → Zustand (simple) | Redux Toolkit (complex)
Atomic/granular      → Jotai
Server/remote data   → React Query (TanStack Query)
URL state            → nuqs, router search params
Form state           → React Hook Form

Key patterns:

  • Zustand: create()(devtools(persist((set) => ({...})))) -- use slices for scale, selective subscriptions to prevent re-renders
  • React Query: query keys factory (['users', 'detail', id] as const), staleTime/gcTime, optimistic updates with onMutate/onError rollback
  • Separate client state (Zustand) from server state (React Query) -- never duplicate server data in client store
  • Colocate state close to where it's used; don't over-globalize

Performance

Critical -- eliminate waterfalls:

  • Promise.all() for independent async operations
  • Move await into branches where actually needed
  • Suspense boundaries to stream slow content

Critical -- bundle size:

  • Import directly from modules, avoid barrel files (index.ts re-exports)
  • next/dynamic or React.lazy() for heavy components
  • Defer third-party scripts (analytics, logging) until after hydration
  • Preload on hover/focus for perceived speed
  • content-visibility: auto + contain-intrinsic-size on long lists -- skips off-screen layout/paint

Re-render optimization:

React Compiler (React 19): auto-memoizes -- write idiomatic React, remove manual useMemo/useCallback/memo. Enable via framework config (Next.js: reactCompiler: true in next.config). Non-framework: install babel-plugin-react-compiler. Keep components pure.

React 19

Next.js App Router

File conventions: page.tsx (route UI), layout.tsx (shared wrapper), template.tsx (re-mounted on navigation, unlike layout), loading.tsx (Suspense), error.tsx (error boundary), not-found.tsx (404), default.tsx (parallel route fallback), route.ts (API endpoint)

Rendering modes: Server Components (default) | Client ('use client') | Static (build) | Dynamic (request) | Streaming (progressive)

Decision: Server Component unless it needs hooks, event handlers, or browser APIs. Split: server parent + client child. Isolate interactive components as 'use client' leaf components -- keep server components static with no global state or event handlers.

Routing patterns:

Caching:

Data fetching: Fetch in Server Components where data is used. Use Suspense boundaries for slow queries. React.cache() for per-request dedup. generateStaticParams for static generation. generateMetadata for dynamic SEO. Static metadata with title: { default: 'App', template: '%s | App' } for cascading page titles. after() for non-blocking side effects (logging, analytics) -- runs after response is sent. Hoist static I/O (fonts, config) to module level -- runs once, not per request.

Testing (Vitest + React Testing Library)

General testing discipline (anti-patterns, rationalization resistance): see ia-writing-tests skill.

See testing patterns and examples for component, hook, and mocking examples.

See e2e testing for Playwright patterns.

Tailwind Integration

For Tailwind v4 configuration, utility patterns, dark mode, and component variants, see ia-tailwind-css skill.

Class sorting in JSX: when using clsx, cva, cn, tv, or tw utility functions, keep Tailwind classes in canonical order. Configure eslint-plugin-better-tailwindcss with useSortedClasses and functions: ["clsx", "cva", "cn", "tv", "tw"] to enforce this automatically across JSX attributes and helper calls.

Discipline

  • Simplicity first -- every change as simple as possible, impact minimal code
  • Only touch what's necessary -- avoid introducing unrelated changes
  • No hacky workarounds -- if a fix feels wrong, step back and implement the clean solution
  • Before adding a new abstraction, verify it appears in 3+ places

References

Verify

  • TypeScript compiles with zero errors
  • No suppressed lint rules (eslint-disable, @ts-ignore) in new code
  • useEffect dependency arrays not manually overridden
  • No forwardRef usage in React 19+ projects (use ref prop directly)

版本历史

共 2 个版本

  • v4.1.0 当前
    2026-06-03 12:41
  • v3.0.5
    2026-05-01 02:51 安全 安全

安全检测

腾讯云安全 (Keen)

队列中

腾讯云安全 (Sanbu)

队列中

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 677 📥 325,886
dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,421
ai-agent

ia-brainstorming

iliaal
前期探索:深度访谈、方案对比、设计文档。用于对模糊需求或创意进行细化、澄清等场景。
★ 0 📥 854