How to override existing React components
React.js has a component-based architecture that offers a flexible environment for overriding its existing components. It improves the code reusability and provides customization margins by adopting a modular approach to development.
What is overriding?
Overriding is a basic OOP concept in which we can alter the functionality of an existing child class in its parent class. In React, we can define a different implementation for existing components in their parent components.
Overriding existing components
As discussed above, React application modules can be divided into different components to improve reusability. Therefore, to use the existing components we override them. There are three basic approaches that can be used to override existing components in React.
Composition
Inheritance
High-order components
Let's add a simple button inside a navbar using each approach one by one.
Composition
Composition is mainly explained in the context of a 'has-a' relation, where a component is contained by another component. Each component has its own encapsulated behavior and interacts with each other through the reference specified for one component in the other component.
Note: Real life example of a 'has-a' relation can be of a car. Car has a seat or an engine. Seat and engine are small individually useful components that come together to form a Car component.
Example
.nav{
background-color: cadetblue;
align-items: center;
text-align: center;
padding: 10px;
}
.btn{
background-color: rgb(30, 68, 90);
border: solid rgb(30, 68, 90);
color: aliceblue;
border-radius: 10px;
margin-top: 30px;
margin-bottom: 25px;
margin-left: 30px;
padding: 0.3rem 0.5rem;
}
h1{
color: rgb(255, 255, 255);
font-size:xx-large;
font-weight: bolder;
align-self: center;
}Code explanation
Navbar.js:
Line 2: Import
Buttoncomponent to theNavbarcomponent.Line 10:
Buttonis overridden inside theNavbarfunction.
index.js:
Line 10: Import and render
Navbarin the root to see the output on your screen.
Inheritance
Inheritance is a hierarchal relationship between components, where the child component can inherit properties of its parent component. The child component can use the parent component properties according to its requirements.
Example
.nav{
background-color: cadetblue;
align-items: center;
text-align: center;
padding: 10px;
}
.btn{
background-color: rgb(30, 68, 90);
border: solid rgb(30, 68, 90);
color: aliceblue;
border-radius: 10px;
margin-top: 30px;
margin-bottom: 25px;
margin-left: 30px;
padding: 0.3rem 0.5rem;
}
h1{
color: rgb(255, 255, 255);
font-size:xx-large;
font-weight: bolder;
align-self: center;
}Code explanation
Navbar.js:
Line 2: Import
Buttoncomponent to theNavbarcomponent.Line 4:
Navbaris the overridden child component thatextendsButton, which is the parent component, to inherit its properties.Line 5: Use
render()method whenever a component has to be re-rendered.Line 9: Use
super.render()to call the render function ofButton, which is the parent component.
index.js:
Line 10: Import and render
Navbarin the root to see the output on your screen.
High-order components
High-order component is a function that takes Component as a parameter and returns it after incorporating new behavior. It can add additional functionalities to the wrapped component including state management, event handling, and prop manipulation.
Example
import React from 'react';
import Navbar from "./Navbar";
import Button from './Button';
const MyNav = Navbar(Button);
const Base = () => {
return(
<div>
<MyNav/>
</div>
)
}
export default Base;Code explanation
Base.js:
Line 5: Declare the overridden component
myNavand set it equal to the original componentButtoninside theNavbarwrapper function.Line 4: Call the overridden component
myNavthat displays the original and overridden content.
Navbar.js:
Line 3: Pass a
Componentto the high-order componentNavbar.Line 4: Implement the
myNavfunctional component in which the overriding will occur. It will return the re-rendered content to theBasecomponent.Line 8:
Componentis called which is a built-in attribute that represents the component that is to be rendered (seeBase.jsexplanation for further clarification).Line 12: Return the rendered
myNavcomponent that contains the original and overridden content.
index.js:
Line 10: Import and render
Basein the root to see the output on your screen.
Summary
Overriding approach | How it works |
Composition | Import the component to be overridden inside the other component and call it inside the div where it is to be rendered. |
Inheritence | The child class component inherits the properties of the parent class component and calls super.render() to render its content. |
High order componenets | The built-in component that represents the component to be rendered is passed to the higher-order component. |
Common query
Can inheritance be done using functions instead of classes in React?
Free Resources