React & Frontend Patterns
Central Hub for React Best Practices
This skill provides guidelines for building production-ready React applications. It is designed to be modular: read specific reference files for deep dives.
📚 Quick Reference
1. Component Patterns
- •Design Rules: "Props down, events up", composition over inheritance.
- •Composition: Compound components, Render props (legacy/niche).
- •TypeScript: Proper typing for props and children.
2. State Management
- •Selection: When to use
useStatevs Context vs Zustand vs React Query. - •Custom Hooks: Logic reuse patterns (
useToggle,useDebounce). - •Forms: Controlled components vs libraries.
3. Performance
- •Optimization: Measure first, then optimize.
- •Techniques: React Compiler, Memoization (
useMemo,useCallback), Virtualization. - •Code Splitting:
React.lazy,Suspense.
4. React 19 & Next.js
- •New Hooks:
useActionState,useOptimistic,use. - •Server Components: Architecture and data fetching patterns.
- •Next.js: Next.js specific patterns.
5. Quality Assurance
- •Testing: Unit vs Integration vs E2E.
- •Accessibility: Semantic HTML, Keyboard navigation.
- •Error Handling: Error Boundaries.
🧩 Code Examples
Don't reinvent the wheel. Copy these patterns:
- •Custom Hooks:
useToggle,useDebounce. - •Complex Form: Controlled form with validation.
- •Composition: Compound components & Render props.
- •State Reducer: Context + useReducer pattern.
🚫 Common Anti-Patterns
| ❌ Anti-Pattern | ✅ Solution |
|---|---|
| Prop Drilling (passing props 3+ levels) | Use Composition or Context. |
| Giant Components (> 200 lines) | Split into smaller, focused components. |
useEffect chaining | Use derived state or event handlers. |
| Premature Optimization | Profile first. Most code doesn't need memoization. |
Index as Key (key={index}) | Use stable unique IDs. |
| Storing Props in State | Use props directly or derive state. |
Fetching in useEffect (client) | Use React Query or SWR. |
💡 Quick Tips
- •Readability > Cleverness: Clear code is better than short code.
- •Colocation: Keep styles, tests, and types close to the component.
- •Automatic Batching: React 18+ batches updates automatically. You rarely need to batch manually.
- •Strict Mode: Always develop with Strict Mode enabled to catch bugs early.