Component Patterns
This skill provides pragmatic patterns for React components.
1. Recommended component structure
Order (top to bottom):
- •Types (
Props) - •Hooks/state
- •Derived values (
useMemowhere useful) - •Handlers (
useCallbackwhen passed to children) - •Render
2. Props and boundaries
- •Prefer small
Propsand explicit types. - •Avoid passing "bags of data"; pass exactly what the component needs.
- •Keep components pure: do not fetch data directly inside presentational components unless that is the design.
3. Composition
- •Prefer composition over deep prop drilling:
- •
childrenslots - •context providers (sparingly, with clear boundaries)
- •
- •Keep reusable components in a shared
components/directory; keep domain-specific UI infeatures/<feature>/.
4. Common anti-patterns
- •Inline anonymous functions in deep lists without need
- •Multiple early returns that change layout unpredictably
- •Doing expensive computation during render without memoization