Reusable components are the most powerful feature in React.js. It saves developers time as they don’t have to write multiple codes for the same functionality in the web applications. However, many developers still struggle to maintain a clean code structure while creating reusable components.
In this article, we will learn about Inheritance and Compositions that are used for creating reusable components in React.js. Also, find out why to use composition over inheritance to make the project file structure easier to read and maintain.
So let’s get started!
Both Inheritance and Compositions are means of transmitting attributes shared by multiple objects in the application, we will look into each of them in detail.
Inheritance is a technique in object-oriented programming, it is referred to when one class inherits the class properties to duplicate the behavior while allowing to add the extra features. This ability is used while building React components to achieve code reusability.
Let’s assume that you have a React Component called SubmitButton which is created by extending the Button component with the properties- background: pink and color: black. Then the instance of the SubmitButton will inherit the behavior of a Button instance.
The functionality is quite similar to inheritance, however rather than inheriting the properties of the parent class it represents a class that references one or more objects of the other class as an instance. This way it provides the new behavior to the class or object rather than extending it in the hierarchy.
If we implement the SubmitButton using Composition as a component that uses Button. Only the behavior of the Button is passed, while the properties are not inherited.
So if you are new to React.js, it will be hard to choose between Inheritance and Composition, and this is because Inheritance and Composition are quite similar in how the objects are related.
Now, let’s understand what makes them different.
Just consider this example from the React docs, the Dialog component is a special case of components WelcomeDialog. Here the “specific” component can render a “generic” one and configure it with a prop with Composition, which is the better way to use the behavior of one component in the other.
1 function Dialog(props) { 2 return ( 3 <FancyBorder color="blue"> 4 5 <h1 className="Dialog-title"> 6 {props.title} 7 </h1> 8 9 <p className="Dialog-message"> 10 {props.message} 11 </p> 12 </FancyBorder> 13 ); 14 } 15 16 function WelcomeDialog() { 17 return ( 18 <Dialog title="blue" message="Thank you for visiting our spacecraft!"/> 19 ); 20 } 21
Props and Composition give you more flexibility for component customization in all ways. And it also works better for the components defined as classes.
So, if you are building react components, you can make it reusable by extracting the functionality into the separate JavaScript module. Later another component can easily import and use the functionality, object, or class without extending it.
For any application, the project requirements may keep on changing over time. Therefore, it's always a good idea to choose composition over inheritance.
However, if you are looking for the best ways to build a React.js web application, try using the React.js web app builder by DhiWise. The programming platform empowers developers for building scalable web applications faster.
It offers full flexibility to the user to change the UI code conveniently and build the custom web app in a minimum time without affecting the quality.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.