From the course: Build Modern Web Apps with React, Hooks, State Management, and APIs Using Vite or Next.js

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Solution: Component composition

Solution: Component composition

Welcome back. Let's discuss the solutions for prop drilling. Solution number one is component composition. We need to design components to be more flexible and self-contained. Remember that prop drilling is a problem we always want to avoid. Its solutions are designed in such a way that we have to change our code and use this approach. The component composition gives a more cleaner, flexible, and no intermediate props way of solving prop drilling problem. Consider this example where we are avoiding prop drilling using a card component which is receiving a children prop. That means anything can render under the hierarchy of card component and only the component which actually needs the user props are receiving it. Let's understand this via a code example. We will develop a prop drilling problem first using the previous video example of parent, child and grandchild example. After that, we'll fix that problem using component composition. So let's continue to the code. Before starting…

Contents