Key takeaways
Container/presentational components: Separate your components into container components and presentational components. Container components handle the application's state and logic, while presentational components focus on how things look. This improves code organization, reusability, and maintainability.
Render props: Pass a function as a prop to a component to control what gets rendered. This allows components to share code, logic, or state with other components.
Consumer pattern: Use the Context API to separate data producers (providers) and consumers. Consumers can access data provided by a context without explicitly passing it through props. This improves maintainability and reduces prop drilling.
React patterns refer to recurring solutions or best practices that developers adopt when designing and structuring React components. These patterns help improve code organization, maintainability, and reusability. Here are some common React patterns:
Container/presentational components
Container components manage the state and logic of an application. They are responsible for data fetching, handling user input, and any other business logic. Presentational components are concerned with how things look. They receive data and callbacks as props from container components and focus on rendering the UI. Separating components into container and presentational components is a common practice in React to improve code organization, reusability, and maintainability. This separation follows the principle of having one component responsible for managing state and logic (container) while another focuses solely on presenting UI (presentational).
Let's take a look at the following example: