React Idioms and Patterns
React 18+ rewards composition, hooks, and server components. Idiomatic React = functional, performant, accessible.
Scope: React-specific patterns. For TypeScript:
@.gemini/skills/typescript-idioms/SKILL.md. For general frontend:@.gemini/skills/frontend-design/SKILL.md.
Component Patterns
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:
<ErrorBoundary fallback={<ErrorMessage />}> <TaskList /> </ErrorBoundary>
Hooks
Custom hooks for reusable logic:
function useTask(id: string) { const { data, error, isLoading } = useSWR(`/api/tasks/${id}`, fetcher); return { task: data, error, isLoading }; }useMemo/useCallbackonly for measured performance issues — not by default.useEffectcleanup — always return cleanup function for subscriptions.
State Management
- Local state first (
useState), lift only when needed. - Server state: TanStack Query / SWR — never in global state.
- Client state: Context for small, Zustand/Jotai for complex.
Performance
React.memoonly when profiling shows unnecessary re-renders.- Code splitting:
React.lazy+Suspensefor route-level splitting. - Virtual scrolling for long lists (TanStack Virtual).
Testing
React Testing Library + Vitest/Jest. Test behavior, not implementation.
test('displays task title', () => {
render(<TaskCard task={mockTask} />);
expect(screen.getByText('Deploy fix')).toBeInTheDocument();
});
Related
- TypeScript Idioms @.gemini/skills/typescript-idioms/SKILL.md
- Frontend Design @.gemini/skills/frontend-design/SKILL.md
- Accessibility Principles @.gemini/skills/accessibility-principles/SKILL.md