用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/irahardianto/awesome-agv --skill react-idioms命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Structured logging implementation patterns: log levels, mandatory context fields (correlationId, userId, duration), security (PII scrubbing), and per-language library choices (Go slog, TypeScript pino, Python structlog). Load when implementing logging in any operation entry point. Prerequisite: logging-and-observability-mandate.md.
Python type hints, Protocols, Pydantic, async/await, pytest, ruff, mypy strict.
Go stdlib, error wrapping, interfaces, goroutines, table-driven tests, gofumpt.
基于 SOC 职业分类
正在显示 SKILL.md
| name | react-idioms |
| description | React hooks, Suspense, Server Components, React 19 patterns. For TypeScript see typescript-idioms. |
| paths | ["**/*.jsx","**/*.tsx"] |
React 19+ rewards composition, hooks, and server-aware patterns. Idiomatic React = functional, performant, accessible. Prefer co-located features, custom hooks for logic reuse, and server state libraries over hand-rolled fetch logic.
Scope: This file covers React-specific coding idioms for components, hooks, state, routing, and forms. For TypeScript type system patterns, see
@.agents/skills/typescript-idioms/SKILL.md. For file and folder layout, seereferences/project-structure.md(and the shared@.agents/skills/frontend-design/references/frontend-layout.md). For general frontend design, see@.agents/skills/frontend-design/SKILL.md.Loading guard: If the project uses Next.js (App Router —
app/dir ornext.config.*), load@.agents/skills/nextjs-idioms/SKILL.mdinstead of this skill for App-Router-specific patterns. This skill still applies to client components and pure-React (Vite) SPAs.
Load these before writing code in the matching context — not after.
| Situation | Reference to Load |
|---|---|
| Starting a React (Vite) project or reviewing file layout | references/project-structure.md + @.agents/skills/frontend-design/references/frontend-layout.md |
| TypeScript type system, async, Zod, error types | @.agents/skills/typescript-idioms/SKILL.md (always co-load) |
| Zod schemas / boundary validation | @.agents/skills/typescript-idioms/references/zod-patterns.md |
| Async / I/O / coercion pitfalls | @.agents/skills/typescript-idioms/references/ts-patterns-and-anti-patterns.md |
| Next.js App Router (RSC, Server Actions, caching) | @.agents/skills/nextjs-idioms/SKILL.md (use that skill instead for Next projects) |
Functional components only — no class components in new code.
Composition over inheritance:
// ✅ Compound components
<Card>
<Card.Header>{title}</Card.Header>
<Card.Body>{children}</Card.Body>
</Card>
Error boundaries for graceful failure — wrap feature subtrees to catch render errors.
Render props for flexible, headless composition:
<DataLoader url="/api/tasks">
{({ data, isLoading, error }) => {
if (isLoading) return <Skeleton />;
if (error) return <ErrorMessage error={error} />;
return <TaskList tasks={data} />;
}}
</DataLoader>
Props typing — always explicit:
// ✅ Typed props with defaults
interface TaskCardProps {
task: Task;
onComplete?: (taskId: ) => ;
?: | ;
}
() {
}
Custom hooks for reusable logic:
function useTask(id: string) {
const { data, error, isLoading } = useQuery({
queryKey: ['task', id],
queryFn: () => taskApi.getTask(id),
});
return { task: data, error, isLoading };
}
useMemo/useCallback only for measured performance issues — not by default.
useEffect cleanup — always return cleanup function for subscriptions:
useEffect(() => {
const controller = new AbortController();
fetchTasks(controller.signal).then(setTasks);
return () => controller.abort(); // ✅ Cleanup on unmount
}, []);
useRef for values that don't trigger re-renders:
// ✅ Timer ref — doesn't cause re-render
const timerRef = useRef<ReturnType<typeof setInterval>>();
useEffect(() => {
timerRef.current = setInterval(pollStatus, 5000);
return () => (timerRef.);
}, []);
use() hook — read resources, promises, and context directly in render:
// ✅ Read a promise during render (replaces useEffect + useState)
function TaskDetail({ taskPromise }: { taskPromise: Promise<Task> }) {
const task = use(taskPromise);
return <h1>{task.title}</h1>;
}
// ✅ Read context without useContext
function TaskActions() {
const theme = use(ThemeContext);
return <button className={theme.primaryBtn}>Save</button>;
}
useActionState for form actions (replaces useFormState):
// ✅ Server-aware form with pending state
async function createTask(_prev: State, formData: FormData) {
const result = await api.createTask(Object.fromEntries(formData));
return result.error ? { error: result.error } : { : };
}
() {
[state, formAction, isPending] = (createTask, { : });
(
);
}
React Hook Form + Zod for validated forms:
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
const taskSchema = z.object({
title: z.string().min(1, 'Title is required').max(200),
priority: z.enum(['low', 'medium', 'high']),
});
type TaskFormData = z.infer<typeof taskSchema>;
function TaskForm({ onSubmit }: { onSubmit: (data: TaskFormData) => Promise<void> }) {
const { register, handleSubmit, formState: { errors } } = useForm<TaskFormData>({
resolver: zodResolver(taskSchema),
});
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register('title')} />
{errors.title && <p>{errors.title.message}</>}
Create
);
}
React Router 7 data patterns — loaders and actions:
// ✅ Route-level data loading
export async function loader({ params }: LoaderFunctionArgs) {
return taskApi.getTask(params.id!);
}
export function TaskPage() {
const task = useLoaderData<typeof loader>();
return <TaskDetail task={task} />;
}
TanStack Router for type-safe routes:
const taskRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/tasks/$taskId',
loader: ({ params }) => taskApi.getTask(params.taskId),
component: TaskPage,
});
Route-level code splitting — always lazy-load route components with React.lazy + Suspense (see Performance section).
Decision tree:
useState→useContext→ Zustand → TanStack Query (for server state)
useState), lift only when shared by siblings.// ✅ Server state managed by TanStack Query
function useTasks() {
return useQuery({
queryKey: ['tasks'],
queryFn: () => taskApi.getTasks(),
staleTime: 5 * 60 * 1000,
});
}
// ✅ features/task/store/task.store.ts — Zustand for UI-only state
import { create } from 'zustand';
interface TaskUIState {
selectedId: string | null;
filter: 'all' | 'active' | 'done';
selectTask: (id: string | null) => void;
setFilter: (f: TaskUIState['filter']) => void;
}
export const useTaskUIStore = create<TaskUIState>(() => ({
: ,
: ,
: ({ : id }),
: ({ filter }),
}));
() {
{ filter, setFilter } = ();
;
}
For universal error handling principles, see
.agents/rules/error-handling-principles.md.
Error boundaries for component tree errors — use react-error-boundary or a custom class component:
// ✅ Wrap feature subtrees, log in componentDidCatch
<ErrorBoundary fallback={<ErrorMessage />}>
<TaskList />
</ErrorBoundary>
TanStack Query — use retry, isError, and error from query result (see State Management).
Log errors in componentDidCatch with correlationId and componentStack — never swallow silently.
React.memo only when profiling shows unnecessary re-renders.React.lazy + Suspense for route-level splitting:
import { lazy, Suspense } from 'react';
const TaskPage = lazy(() => import('./features/task/TaskPage'));
const ProfilePage = lazy(() => import('./features/profile/ProfilePage'));
function AppRoutes() {
return (
<Suspense fallback={<PageSkeleton />}>
<Routes>
<Route path="/tasks" element={<TaskPage />} />
<Route path="/profile" element={<ProfilePage />} />
</Routes>
</Suspense>
);
}
loading="lazy" and srcSet for responsive images.useEffect for data fetching — use TanStack Query, SWR, or loaderskey={index} on dynamic lists — use stable, unique identifiersuseMemo/useCallback on everything — premature optimization// ❌ Unnecessary state
const [filteredTasks, setFilteredTasks] = useState<Task[]>([]);
useEffect(() => {
setFilteredTasks(tasks.filter(t => t.status === filter));
}, [tasks, filter]);
// ✅ Computed during render — no extra state
const filteredTasks = tasks.filter(t => t.status === filter);
useFormState — replaced by useActionState in React 19For universal testing principles, see
.agents/rules/testing-strategy.md. Below: React-specific patterns only.
React Testing Library + Vitest/Jest. Test behavior, not implementation.
Component rendering and interaction:
import { render, screen, fireEvent } from '@testing-library/react';
test('displays task title', () => {
render(<TaskCard task={mockTask} />);
expect(screen.getByText('Deploy fix')).toBeInTheDocument();
});
test('calls onComplete when button clicked', async () => {
const onComplete = vi.fn();
render(<TaskCard task={mockTask} onComplete={onComplete} />);
await fireEvent.click(screen.getByRole('button', { name: /complete/i }));
expect(onComplete).toHaveBeenCalledWith(mockTask.id);
});
Provider wrapper for tests — wrap components that depend on providers:
function createTestWrapper() {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: } } });
(
);
}
(, { : () });
| Tool | Purpose | Command |
|---|---|---|
| Prettier | Formatting | npx prettier --write . |
| ESLint + eslint-plugin-react-hooks | Linting | npx eslint . |
| TypeScript | Type checking | npx tsc --noEmit |
One concern per component — if a component exceeds ~100 JSX lines, extract a sub-component.
useOptimistic for instant UI feedback:
const [optimisticTasks, addOptimistic] = useOptimistic(
tasks,
(state, newTask: Task) => [...state, newTask],
);
// Call addOptimistic(tempTask) before await api.createTask(tempTask)
<form action={fn}> for progressive enhancement — works before JS loads (see useActionState example above).
Controlled vs uncontrolled decision:
register) for simple forms — better performance, less boilerplateController) when the UI must react to every keystroke (live previews, dependent fields)// ✅ features/task/api/task.api.ts — interface
export interface TaskAPI {
getTasks(): Promise<Task[]>;
createTask(data: CreateTaskDTO): Promise<Task>;
}
// ✅ features/task/api/task.api.backend.ts — production (implements TaskAPI with fetch)
// ✅ features/task/api/task.api.mock.ts — test (implements TaskAPI with in-memory data)
useMemo.Testing custom hooks with renderHook:
import { renderHook, waitFor } from '@testing-library/react';
test('useTask returns task data', async () => {
const { result } = renderHook(() => useTask('1'), {
wrapper: createTestWrapper(),
});
await waitFor(() => expect(result.current.task).toBeDefined());
expect(result.current.task?.title).toBe('Deploy fix');
});
MSW for API mocking — intercept at the network level:
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer(
http.get('/api/tasks', () =>
HttpResponse.json([{ id: '1', title: 'Deploy fix', status: 'todo' }])
),
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());