--- name: react-component-architecture description: Design scalable React components using functional components, hooks, composition patterns, and TypeScript. Use when building reusable component libraries and maintainable UI systems. --- # React Component Architecture ## Overview Build scalable, maintainable React components using modern patterns including functional components, hooks, composition, and TypeScript for type safety. ## When to Use - Component library design - Large-scale React applications - Reusable UI patterns - Custom hooks development - Performance optimization ## Implementation Examples ### 1. **Functional Component with Hooks** ```typescript // Button.tsx import React, { useState, useCallback } from 'react'; interface ButtonProps { variant?: 'primary' | 'secondary' | 'danger'; size?: 'sm' | 'md' | 'lg'; disabled?: boolean; onClick?: () => void; children: React.ReactNode; } export const Button: React.FC = ({ variant = 'primary', size = 'md', disabled = false, onClick, children }) => { const variantStyles = { primary: 'bg-blue-500 hover:bg-blue-600', secondary: 'bg-gray-500 hover:bg-gray-600', danger: 'bg-red-500 hover:bg-red-600' }; const sizeStyles = { sm: 'px-2 py-1 text-sm', md: 'px-4 py-2 text-base', lg: 'px-6 py-3 text-lg' }; return ( ); }; ``` ### 2. **Custom Hooks Pattern** ```typescript // useFormInput.ts import { useState, useCallback } from 'react'; interface UseFormInputOptions { initialValue?: string; validator?: (value: string) => string | null; } export const useFormInput = (options: UseFormInputOptions = {}) => { const [value, setValue] = useState(options.initialValue || ''); const [error, setError] = useState(null); const validate = useCallback(() => { if (options.validator) { const validationError = options.validator(value); setError(validationError); return !validationError; } return true; }, [value, options.validator]); const reset = useCallback(() => { setValue(options.initialValue || ''); setError(null); }, [options.initialValue]); return { value, setValue, error, validate, reset, bind: { value, onChange: (e: React.ChangeEvent) => setValue(e.target.value) } }; }; // Usage const MyForm: React.FC = () => { const email = useFormInput({ validator: (v) => !v.includes('@') ? 'Invalid email' : null }); return (
{email.error && {email.error}}
); }; ``` ### 3. **Composition Pattern** ```typescript // Card.tsx interface CardProps { children: React.ReactNode; className?: string; } const Card: React.FC = ({ children, className = '' }) => (
{children}
); const CardHeader: React.FC = ({ children }) => (
{children}
); const CardBody: React.FC = ({ children }) => (
{children}
); const CardFooter: React.FC = ({ children }) => (
{children}
); // Compound component export { Card }; Card.Header = CardHeader; Card.Body = CardBody; Card.Footer = CardFooter; // Usage Title Content Actions ``` ### 4. **Higher-Order Component (HOC)** ```typescript // withLoader.tsx interface WithLoaderProps { isLoading: boolean; error?: Error | null; } function withLoader

( Component: React.ComponentType

): React.FC

{ return ({ isLoading, error, ...props }: P & WithLoaderProps) => { if (isLoading) return

Loading...
; if (error) return
{error.message}
; return ; }; } // Usage const UserList: React.FC<{ users: User[] }> = ({ users }) => (
    {users.map(u =>
  • {u.name}
  • )}
); export const LoadingUserList = withLoader(UserList); ``` ### 5. **Render Props Pattern** ```typescript // DataFetcher.tsx interface DataFetcherProps { url: string; children: (data: T | null, loading: boolean, error: Error | null) => React.ReactNode; } export const DataFetcher = ({ url, children }: DataFetcherProps) => { const [data, setData] = React.useState(null); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(null); React.useEffect(() => { fetch(url) .then(r => r.json()) .then(setData) .catch(setError) .finally(() => setLoading(false)); }, [url]); return <>{children(data, loading, error)}; }; // Usage url="/api/users"> {(users, loading, error) => ( <>{loading ?

Loading...

: users?.map(u =>

{u.name}

)} )} ``` ## Best Practices - Use TypeScript for type safety - Implement proper prop validation - Keep components focused and single-purpose - Leverage hooks for state and side effects - Use composition over inheritance - Memoize expensive computations - Extract custom hooks for reusable logic ## Resources - [React Documentation](https://react.dev) - [React Hooks API](https://react.dev/reference/react) - [TypeScript with React](https://www.typescriptlang.org/docs/handbook/react.html)