¡Stop passing props between React components! Apply composition pattern
<p>Passing props between React components is a common practice when building complex applications. While <strong>it works well for small-scale applications</strong>, it can quickly become <strong>unmanageable as the application grows</strong> larger. Passing props down several levels of components can make it <strong>difficult to maintain and debug the code</strong>. In this article, we’ll explore the composition pattern and how it can help you organize your code and reduce the number of props you need to pass between components. By the end of this article, you’ll have a better understanding of the composition pattern and <strong>how to apply it to your React applications</strong>.</p>
<h2>Dependencies:</h2>
<p><em>Styled Components for fast styling:</em></p>
<blockquote>
<p><strong>npm install styled-components</strong></p>
<p><strong>npm install -D @types/styled-components</strong></p>
</blockquote>
<h2>Bad: Inheritance Pattern</h2>
<p>In this example we will show a grid of products brought from a fake api, each product will have a thumbnail, title, description, rating, stock and price.</p>
<h2>Getting products</h2>
<p>First, we’ll create a custom hook to get the products and load status and error if the fetch fails.</p>
<p>In this example I’m using typescript, the schema I’m importing is just a typescript interface to define the product, but you can use javascript if you want.</p>
<p><a href="https://medium.com/@flavtech/stop-passing-props-between-react-components-apply-composition-pattern-15b132bf0686">Visit Now</a></p>