Course contentsShow
← React
Lesson 189 of 2,3966. Component BasicsFree lesson

Composition Over Inheritance in React

Why React favors composition patterns instead of classical inheritance for code reuse and flexibility.

Why React Favors Composition Over Inheritance

What you'll learn: Why React uses composition patterns instead of classical inheritance to build flexible, reusable components.

In traditional object-oriented programming, you might use class inheritance to share behavior—a Dog class extends an Animal class. React takes a different approach: composition, where you build complex components by combining simpler ones, rather than creating parent-child class hierarchies.

The React Philosophy

React's component model is built around the idea that components should be assembled like LEGO blocks, not extended through inheritance chains. You've already been doing this when you call one component from inside another—that's composition in action.

Think of it like cooking: inheritance is saying "a pizza is a type of bread, which is a type of baked good." Composition is saying "a pizza is made by combining dough, sauce, and toppings." The second approach is more flexible—you can easily swap ingredients without restructuring your entire recipe hierarchy.

Why Composition Wins

In React, if you want to reuse logic or UI, you compose components together rather than extending them. Need a button with an icon? Compose a Button component that can accept an Icon component. Need special behavior? Pass different functions as props. Need to wrap content? Use the children pattern.

The React team has consistently stated: there are no use cases where inheritance is recommended. Every problem inheritance solves in other frameworks, React solves more elegantly through composition patterns.

This isn't a limitation—it's a design choice that keeps your component tree predictable, avoids deep inheritance chains, and makes code easier to understand and refactor.

Key Takeaway: React replaces class inheritance with component composition—building complex UIs by combining simple, focused components rather than extending base classes.