--- name: frontend-component-patterns description: Build reusable, composable, and maintainable React/Vue/Angular components following established design patterns like compound components, render props, custom hooks, and HOCs. Use when creating component libraries, implementing component composition, building reusable UI elements, designing prop APIs, managing component state patterns, implementing controlled vs uncontrolled components, creating compound components, using render props or children as functions, building custom hooks, or establishing component architecture standards. --- # Frontend Component Patterns - Building Reusable React Components ## When to use this skill - Creating reusable component libraries - Implementing component composition patterns - Building flexible, configurable UI components - Designing intuitive component prop APIs - Managing component state with patterns - Implementing controlled vs uncontrolled components - Creating compound components (e.g., Tabs, Accordion) - Using render props or children as functions - Building custom React hooks for shared logic - Implementing Higher-Order Components (HOCs) - Establishing component architecture standards - Creating accessible, keyboard-navigable components ## When to use this skill - Designing React component architecture, improving component reusability, managing state, or solving common UI patterns. - When working on related tasks or features - During development that requires this expertise **Use when**: Designing React component architecture, improving component reusability, managing state, or solving common UI patterns. ## Core Principles 1. **Composition Over Inheritance** - Build complex UIs from simple components 2. **Single Responsibility** - Each component does one thing well 3. **Props Down, Events Up** - Unidirectional data flow 4. **Separation of Concerns** - Logic separate from presentation 5. **Accessibility First** - ARIA, keyboard navigation, semantic HTML ## Component Patterns ### 1. **Presentational vs Container Components** ```typescript // ❌ Mixed concerns - logic + presentation together function UserProfile() { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { fetch(`/api/users/${userId}`) .then(r => r.json()) .then(setUser) .finally(() => setLoading(false)); }, [userId]); if (loading) return
Loading...
; return (
{user.name}

{user.name}

{user.bio}

); } // ✅ Separated - Container handles logic function UserProfileContainer({ userId }: { userId: string }) { const { data: user, isLoading } = useUser(userId); if (isLoading) return ; if (!user) return ; return ; } // ✅ Presentational - Pure display component interface UserProfileViewProps { user: { avatar: string; name: string; bio: string; }; } function UserProfileView({ user }: UserProfileViewProps) { return (
{user.name}

{user.name}

{user.bio}

); } ``` ### 2. **Compound Components** ```typescript // ✅ Flexible, composable API interface TabsProps { children: React.ReactNode; defaultValue?: string; } interface TabsContextValue { activeTab: string; setActiveTab: (value: string) => void; } const TabsContext = React.createContext(null); function Tabs({ children, defaultValue }: TabsProps) { const [activeTab, setActiveTab] = useState(defaultValue || ''); return (
{children}
); } function TabsList({ children }: { children: React.ReactNode }) { return
{children}
; } function TabsTrigger({ value, children }: { value: string; children: React.ReactNode }) { const context = useContext(TabsContext); if (!context) throw new Error('TabsTrigger must be inside Tabs'); const isActive = context.activeTab === value; return ( ); } function TabsContent({ value, children }: { value: string; children: React.ReactNode }) { const context = useContext(TabsContext); if (!context) throw new Error('TabsContent must be inside Tabs'); if (context.activeTab !== value) return null; return
{children}
; } // Export as compound component export { Tabs, TabsList, TabsTrigger, TabsContent }; // Usage - flexible and intuitive function App() { return ( Account Password Notifications ); } ``` ### 3. **Render Props Pattern** ```typescript // ✅ Flexible rendering with render props interface MousePositionProps { children: (position: { x: number; y: number }) => React.ReactNode; } function MousePosition({ children }: MousePositionProps) { const [position, setPosition] = useState({ x: 0, y: 0 }); useEffect(() => { const handleMove = (e: MouseEvent) => { setPosition({ x: e.clientX, y: e.clientY }); }; window.addEventListener('mousemove', handleMove); return () => window.removeEventListener('mousemove', handleMove); }, []); return <>{children(position)}; } // Usage - consumer controls rendering function App() { return ( {({ x, y }) => (
Mouse is at ({x}, {y})
)}
); } ``` ### 4. **Custom Hooks (Modern Alternative)** ```typescript // ✅ Extract reusable logic as hook function useMousePosition() { const [position, setPosition] = useState({ x: 0, y: 0 }); useEffect(() => { const handleMove = (e: MouseEvent) => { setPosition({ x: e.clientX, y: e.clientY }); }; window.addEventListener('mousemove', handleMove); return () => window.removeEventListener('mousemove', handleMove); }, []); return position; } // Usage - cleaner than render props function App() { const { x, y } = useMousePosition(); return (
Mouse is at ({x}, {y})
); } // ✅ More custom hooks examples function useDebounce(value: T, delay: number): T { const [debouncedValue, setDebouncedValue] = useState(value); useEffect(() => { const timer = setTimeout(() => setDebouncedValue(value), delay); return () => clearTimeout(timer); }, [value, delay]); return debouncedValue; } function useLocalStorage(key: string, initialValue: T) { const [value, setValue] = useState(() => { const stored = localStorage.getItem(key); return stored ? JSON.parse(stored) : initialValue; }); useEffect(() => { localStorage.setItem(key, JSON.stringify(value)); }, [key, value]); return [value, setValue] as const; } ``` ### 5. **Higher-Order Components (Legacy Pattern)** ```typescript // ✅ HOC for adding functionality function withLoading

( Component: React.ComponentType

) { return function WithLoadingComponent( props: P & { isLoading: boolean } ) { if (props.isLoading) { return ; } return ; }; } // Usage const UserListWithLoading = withLoading(UserList); // Note: Custom hooks are now preferred over HOCs ``` ## State Management Patterns ### 1. **Props vs State** ```typescript // ✅ Props - passed from parent interface ButtonProps { label: string; // Display text onClick: () => void; // Callback disabled?: boolean; // Optional config } function Button({ label, onClick, disabled }: ButtonProps) { return ( ); } // ✅ State - internal to component function Counter() { const [count, setCount] = useState(0); // Local state return (

Count: {count}

); } // ✅ Controlled vs Uncontrolled // Controlled - value from props (parent controls) function ControlledInput({ value, onChange }: { value: string; onChange: (value: string) => void; }) { return ( onChange(e.target.value)} /> ); } // Uncontrolled - internal state (component controls) function UncontrolledInput() { const inputRef = useRef(null); const handleSubmit = () => { console.log(inputRef.current?.value); }; return ( <> ); } ``` ### 2. **Lifting State Up** ```typescript // ❌ Duplicated state - siblings can't communicate function ParentBad() { return ( <> {/* Has own search state */} {/* Has own search state */} ); } // ✅ Shared state in parent function ParentGood() { const [searchQuery, setSearchQuery] = useState(''); return ( <> ); } ``` ### 3. **Context for Deep Props** ```typescript // ❌ Prop drilling - passing through many levels function App() { const [theme, setTheme] = useState('light'); return ; } function Layout({ theme, setTheme }) { return ; } function Sidebar({ theme, setTheme }) { return ; } // ✅ Context - direct access at any level interface ThemeContextValue { theme: string; setTheme: (theme: string) => void; } const ThemeContext = createContext(null); function ThemeProvider({ children }: { children: React.ReactNode }) { const [theme, setTheme] = useState('light'); return ( {children} ); } function useTheme() { const context = useContext(ThemeContext); if (!context) throw new Error('useTheme must be inside ThemeProvider'); return context; } // Usage - no prop drilling function ThemeToggle() { const { theme, setTheme } = useTheme(); return ( ); } ``` ## Performance Optimization ### 1. **React.memo - Prevent Re-renders** ```typescript // ❌ Re-renders on every parent render function ExpensiveComponent({ data }: { data: string }) { console.log('Rendering...'); return
{data}
; } // ✅ Only re-renders when props change const ExpensiveComponent = memo(function ExpensiveComponent({ data }: { data: string }) { console.log('Rendering...'); return
{data}
; }); // ✅ Custom comparison function const ExpensiveList = memo( function ExpensiveList({ items }: { items: Item[] }) { return
    {items.map(item =>
  • {item.name}
  • )}
; }, (prevProps, nextProps) => { // Only re-render if items array length changed return prevProps.items.length === nextProps.items.length; } ); ``` ### 2. **useMemo - Cache Expensive Calculations** ```typescript function ProductList({ products, filters }: { products: Product[]; filters: Filters }) { // ❌ Recalculates on every render const filteredProducts = products.filter(p => matchesFilters(p, filters)); // ✅ Only recalculates when dependencies change const filteredProducts = useMemo(() => { return products.filter(p => matchesFilters(p, filters)); }, [products, filters]); return (
{filteredProducts.map(p => ( ))}
); } ``` ### 3. **useCallback - Stable Function References** ```typescript function Parent() { const [count, setCount] = useState(0); // ❌ New function on every render (causes child re-render) const handleClick = () => { console.log('clicked'); }; // ✅ Stable function reference const handleClick = useCallback(() => { console.log('clicked'); }, []); // No dependencies - never changes return ( <>

{count}

); } const MemoizedChild = memo(function Child({ onClick }: { onClick: () => void }) { console.log('Child rendering'); return ; }); ``` ### 4. **Code Splitting & Lazy Loading** ```typescript // ✅ Lazy load heavy components const HeavyChart = lazy(() => import('./HeavyChart')); const AdminPanel = lazy(() => import('./AdminPanel')); function App() { return ( }> } /> } /> ); } ``` ## Accessibility Patterns ### 1. **Semantic HTML & ARIA** ```typescript // ✅ Accessible button function AccessibleButton({ label, onClick }: { label: string; onClick: () => void }) { return ( ); } // ✅ Accessible modal function Modal({ isOpen, onClose, children }: { isOpen: boolean; onClose: () => void; children: React.ReactNode; }) { useEffect(() => { if (isOpen) { document.body.style.overflow = 'hidden'; // Focus trap, ESC key handler, etc. } return () => { document.body.style.overflow = ''; }; }, [isOpen]); if (!isOpen) return null; return (
e.stopPropagation()}> {children}
); } // ✅ Accessible form function SignupForm() { return (
Please enter a valid email
); } ``` ### 2. **Keyboard Navigation** ```typescript // ✅ Keyboard-accessible dropdown function Dropdown({ options }: { options: string[] }) { const [isOpen, setIsOpen] = useState(false); const [selectedIndex, setSelectedIndex] = useState(0); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' || e.key === ' ') { setIsOpen(!isOpen); } else if (e.key === 'ArrowDown') { setSelectedIndex((i) => Math.min(i + 1, options.length - 1)); } else if (e.key === 'ArrowUp') { setSelectedIndex((i) => Math.max(i - 1, 0)); } else if (e.key === 'Escape') { setIsOpen(false); } }; return (
{isOpen && ( )}
); } ``` ## Component Design Checklist ``` Structure: □ Single responsibility per component □ Presentational vs container separation □ Proper props typing (TypeScript) □ Default props defined □ Prop validation for critical inputs State Management: □ State at lowest necessary level □ Lifted state when needed for sharing □ Context for deep prop drilling □ No prop mutations □ Controlled components for forms Performance: □ memo() for expensive components □ useMemo() for expensive calculations □ useCallback() for stable callbacks □ Code splitting for large components □ Lazy loading for routes Accessibility: □ Semantic HTML elements □ ARIA labels and roles □ Keyboard navigation support □ Focus management □ Screen reader tested Reusability: □ Configurable via props □ Composable with children □ No hardcoded values □ Clear, documented API □ Example usage provided ``` ## Resources - [React Documentation](https://react.dev/) - [React Patterns](https://reactpatterns.com/) - [Compound Components](https://kentcdodds.com/blog/compound-components-with-react-hooks) - [WAI-ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/) --- **Remember**: Great components are simple, reusable, accessible, and performant. Start simple, add complexity only when needed.