--- name: pitfalls-react description: "React component patterns, forms, accessibility, and responsive design. Use when building React components, handling forms, or ensuring accessibility. Triggers on: React component, useEffect, form validation, a11y, responsive, Error Boundary." --- # React Pitfalls Common pitfalls and correct patterns for React development. ## When to Use - Building React components - Implementing form validation - Adding error boundaries - Ensuring accessibility (a11y) - Creating responsive layouts - Reviewing React code ## Workflow ### Step 1: Check Component Patterns Verify loading/error states and data checks. ### Step 2: Verify Form Validation Ensure Zod schemas and proper error display. ### Step 3: Check Accessibility Verify ARIA labels and keyboard navigation. --- ## Component Patterns ```tsx // ✅ Define helpers before use or as exports function formatPrice(price: number) { ... } export default function Component() { // ✅ Check data exists before accessing if (!data) return ; // ✅ useEffect for side effects only useEffect(() => { fetchData(); }, []); // ✅ data-testid on interactive elements return ; } // ❌ WRONG: Defining function in render return ``` ## Responsive Layout ```css /* ✅ Mobile-first breakpoints */ .container { padding: 1rem; } @media (min-width: 768px) { .container { padding: 2rem; } } /* ✅ Touch-friendly button sizes (min 44px) */ .btn { min-height: 44px; min-width: 44px; } /* ✅ Horizontal scroll for data tables on mobile */ .table-container { overflow-x: auto; } ``` ## Accessibility (a11y) ```tsx // ✅ Semantic HTML
...
// Not
// ✅ ARIA labels // ✅ Keyboard navigation