Principles for building production-ready React applications.
| Type | Use | State |
|---|
| Server | Data fetching, static | None |
| Client | Interactivity | useState, effects |
| Presentational | UI display | Props only |
| Container | Logic/state | Heavy state |
- One responsibility per component
- Props down, events up
- Composition over inheritance
- Prefer small, focused components
| Pattern | Extract When |
|---|
| useLocalStorage | Same storage logic needed |
| useDebounce | Multiple debounced values |
| useFetch | Repeated fetch patterns |
| useForm | Complex form state |
- Hooks at top level only
- Same order every render
- Custom hooks start with “use”
- Clean up effects on unmount
| Complexity | Solution |
|---|
| Simple | useState, useReducer |
| Shared local | Context |
| Server state | React Query, SWR |
| Complex global | Zustand, Redux Toolkit |
| Scope | Where |
|---|
| Single component | useState |
| Parent-child | Lift state up |
| Subtree | Context |
| App-wide | Global store |
| Hook | Purpose |
|---|
| useActionState | Form submission state |
| useOptimistic | Optimistic UI updates |
| use | Read resources in render |
- Automatic memoization
- Less manual useMemo/useCallback
- Focus on pure components
- Parent provides context
- Children consume context
- Flexible slot-based composition
- Example: Tabs, Accordion, Dropdown
| Use Case | Prefer |
|---|
| Reusable logic | Custom hook |
| Render flexibility | Render props |
| Cross-cutting | Higher-order component |
| Signal | Action |
|---|
| Slow renders | Profile first |
| Large lists | Virtualize |
| Expensive calc | useMemo |
| Stable callbacks | useCallback |
- Check if actually slow
- Profile with DevTools
- Identify bottleneck
- Apply targeted fix
| Scope | Placement |
|---|
| App-wide | Root level |
| Feature | Route/feature level |
| Component | Around risky component |
- Show fallback UI
- Log error
- Offer retry option
- Preserve user data
| Pattern | Use |
|---|
| Interface | Component props |
| Type | Unions, complex |
| Generic | Reusable components |
| Need | Type |
|---|
| Children | ReactNode |
| Event handler | MouseEventHandler |
| Ref | RefObject |
| Level | Focus |
|---|
| Unit | Pure functions, hooks |
| Integration | Component behavior |
| E2E | User flows |
- User-visible behavior
- Edge cases
- Error states
- Accessibility
| ❌ Don’t | ✅ Do |
|---|
| Prop drilling deep | Use context |
| Giant components | Split smaller |
| useEffect for everything | Server components |
| Premature optimization | Profile first |
| Index as key | Stable unique ID |
Remember: React is about composition. Build small, combine thoughtfully.
Always identify gaps and suggest next steps to users. In case there is no gaps anymore, then AI should clearly state that there is no gap left.