Why would you use a Higher-Order Component in React ?
Last Updated :
18 Mar, 2024
A Higher-Order Component (HOC) in React is used to share common functionality between multiple components. It's like a function that takes a component as an argument and returns a new enhanced component with added features.
By offering a pattern for reusing component logic, HOCs help in crafting more maintainable and cleaner React applications. This article simplifies the concept of HOCs, showcases their benefits, and walks through an example to highlight their practical application.
Steps to Create React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Step 3: After setting up react environment on your system, we can start by creating an App.js file and create a file by the name of components in which we will write our desired function.
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Approach to implement Higher Order Component in React:
- First define a function that accepts a component as its argument.
- Inside the HOC, create a new component class or function that wraps the input component.
- Implement any additional logic or state that you want the original component to have.
- Return the enhanced component from the HOC function.
Example: Illustration to showcase the use of Higher Order Component in react.
JavaScript
// App.js
import React from 'react';
// Higher-Order Component
const withHoverEffect = (WrappedComponent) => {
return class extends React.Component {
state = {
isHovered: false,
};
handleMouseEnter = () => {
this.setState({ isHovered: true });
};
handleMouseLeave = () => {
this.setState({ isHovered: false });
};
render() {
return (
<div
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
>
<WrappedComponent {...this.props}
isHovered={this.state.isHovered} />
</div>
);
}
};
};
// Component without hover effect
const Button = ({ isHovered, onClick }) => {
return (
<button style={{ backgroundColor: isHovered ? 'white' : 'red' }}
onClick={onClick}>
Click Me
</button>
);
};
// Enhance Button component with hover effect using HOC
const ButtonWithHoverEffect = withHoverEffect(Button);
const App = () => {
const handleClick = () => {
alert('Button clicked!');
};
return (
<div>
<h1>Example to showcase <br></br> Higher-Order Component</h1>
<ButtonWithHoverEffect onClick={handleClick} />
</div>
);
};
export default App;
Output:

Benefits of Using Higher-Order Components:
- Code Reusability: Simplifies sharing common functionality across components.
- Separation of Concerns: Helps in separating cross-cutting concerns like logging or data fetching from the main component logic.
- Composition: Enables combining multiple HOCs to enhance a component in a modular way.
- Flexibility: Offers a way to add features to components without altering their original code.
- Easier Testing: Makes it simpler to test behaviors isolated by HOCs since the core component remains unaltered.
Conclusion:
Higher-Order Components provide a versatile method for enhancing and reusing component logic in React applications. By understanding how to effectively use HOCs, developers can build applications that are modular, easy to maintain, and adaptable to changing requirements.
Similar Reads
How to use Higher-Order Components in React ? Higher-Order Component is an advanced function, which reuses component logic in a React Component and returns an enhanced new component. It wraps commonly used processes to allow their use by different components, such as state modification or props change. Components are wrapped in HOCs, whose goal
5 min read
ReactJS Higher-Order Components Higher-order components (HOC) are an advanced technique in React that is used for reusing component logic. It is the function that takes the original component and returns the new enhanced component.It doesnât modify the input component directly. Instead, they return a new component with enhanced be
5 min read
How to use HOCs to reuse Component Logic in React ? In React, making reusable components keeps your code neat. Higher-order components (HOCs) are a smart way to bundle and reuse component logic. HOCs are like magic functions that take a component and give you back an upgraded version with extra powers or information. HOCs can be implemented in a few
4 min read
Why do you need to import React in functional components ? React is a declarative, efficient, and flexible JavaScript library for building user interfaces. Itâs âVâ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. In react, we use JSX, especially in the functional important which is
3 min read
What is a pure functional component in ReactJS ? What is a JavaScript function? A JavaScript function is a block of code designed to perform a particular task which is executed when it is called. How React functional components are similar to JavaScript functions? Conceptually, components are like JavaScript functions. Â A functional component is a
3 min read
Top 5 React UI component Libraries As we all know, JavaScript is the unbeatable king of front-end web development, and JavaScript frameworks are introduced to gain more grip over UI design and functionalities. React is one of the JavaScript libraries that is mainly used for designing front-end and mobile app development. Here in this
6 min read