React design patterns are reusable solutions to common problems that developers face when building React applications. These patterns help improve code organization, maintainability, and scalability. Here are some key React design patterns:
- Description: Break down the UI into smaller, reusable components and compose them together to build complex interfaces.
- Use Case: Building modular and reusable components.
- Example: Combining
Header
,Sidebar
, andMainContent
components to create aLayout
component.
- Description: Separate logic (container components) from UI (presentational components)
- Use Case: Improving code readability and reusability.
- Example: A
UserListContainer
fetches data and passes it to aUserList
presentational component for rendering.
- Controlled: Form data is handled by React state.
- Uncontrolled: Form data is handled by the DOM itself.
- Use Case: Managing form inputs and their state.
- Description: A function that takes a component and returns a new component with additional props or functionality.
- Use Case: Sharing logic across multiple components (e.g., authentication, logging).
- Example:
withAuth(ProfilePage)
to add authentication logic to theProfilePage
component.
- Description: Encapsulate reusable logic in custom hooks.
- Use Case: Sharing logic across multiple components.
- Example: A
useFetch
hook to handle API requests.
- Description: Components that render themselves recursively (e.g., for nested data structures like trees).
- Use Case: Rendering hierarchical data.
- Example:
const TreeNode = ({ node }) => (
<div>
<span>{node.name}</span>
{node.children && node.children.map((child) => (
<TreeNode key={child.id} node={child} />
))}
</div>
);
Partial application is a functional programming technique where a function is pre-filled with some of its arguments, creating a new function that takes the remaining arguments. It allows you to specialize or customize a general function for specific use cases.