Design Patterns
- Overview
- Component
- State Management
- Hooks
- Code Organization
- Data Fetching
- Error Handling
- Performance Optimization
- Routing
- Miscellaneous
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Functional Components | Simplify components by using functions instead of classes |
|
| Higher-Order Components (HOC) | Functions that take a component and return an enhanced component with additional functionality |
|
| Render Props | Components with a function as a prop that allows dynamic content rendering based on the function's output |
|
| Controlled and Uncontrolled Components | Controlled components are fully managed by React state and are preferable for predictable behavior, while uncontrolled components maintain their own state |
|
| Container and Presentational Components | Separates "smart" (container) components, which manage state and business logic, from "dumb" (presentational) components, which focus on rendering UI |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Lifting State Up | When several components need to share data, the state can be lifted to their closest common ancestor and passed as props to maintain a single source of truth |
|
| Context API | Allows state to be shared across components without prop drilling |
|
| State Colocation | Keeps the state as close to where it is needed as possible |
|
| State Reducers | Useful for managing complex state logic, often in conjunction with hooks like useReducer to handle various state transitions in a centralized way |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Custom Hooks | Encapsulate reusable stateful logic into functions that can be shared across multiple components |
|
| UseEffect | React Hook for managing side effects in functional components, such as data fetching or subscriptions. It runs after rendering and can be controlled with a dependency array to determine when it should re-run. A cleanup function can be returned to handle cleanup tasks before the component unmounts or before the effect re-runs |
|
| UseMemo and UseCallback | Optimize performance by memoizing expensive calculations ( |
|
| UseImperativeHandle | Provides a way for functional components to expose imperative methods to parent components,
often
used with |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Folder Structure | Organizes files and directories for better readability and scalability |
|
| Extensible Styles | Modularizes styling in CSS (CSS-in-JS), allowing styles to be scoped to individual components |
|
| Centralized Prop Types | Defines prop types for reusable components in a single location |
|
| Service Layers | Creates a service layer for API calls, caching, or other external resources to keep components focused on UI rendering |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Fetching on Render | Triggers data fetching when a component mounts, using hooks like useEffect |
|
| Fetching on Interaction | Triggers data fetching based on user interactions (e.g., clicking a button) to optimize performance |
|
| Data Fetching with Suspense | Uses React Suspense to manage async data fetching with loading states in a declarative way |
|
| Infinite Scrolling / Pagination | Used for loading data progressively, enhancing performance and user experience with lazy loading patterns |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Error Boundaries | Components that catch JavaScript errors in their child component tree and display a fallback UI |
|
| Try/Catch with Async Functions | Used to handle errors in async data fetching functions, often paired with user notifications for better error management |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Lazy Loading and Code Splitting | Loads only the required modules at a time, often using |
|
| Virtualization | Renders only visible items in a large list, improving performance for large data sets |
|
| Memoization | Optimizes components with |
|
| Throttling and Debouncing | Controls the rate at which functions like API calls are executed, reducing unnecessary re-renders or data fetching |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Nested Routing | Creates a hierarchy of routes, allowing sub-routes to be rendered inside parent routes |
|
| Dynamic Routing | Allows routes to change based on state or data |
|
| Protected Routes | Guards routes that require authentication, often wrapping them with an authentication check component |
|
| Lazy Loading Routes | Delays loading of route-based components until they're needed, improving load times and reducing initial bundle size |
|
| Design Pattern | Definition | Use Cases |
|---|---|---|
| Portals | Renders components outside of the main React tree |
|
| Compound Components | Creates a group of components that work together, like |
|
| Configuration-Driven UI | Allows components to be rendered based on configuration objects, making UI structure flexible and customizable |
|
| Facade | Provides a simplified interface to complex logic, often wrapping multiple hooks or components for simplified usage |
|