React JS: Learning Higher Order Components (HOC) in 20 Minutes for Beginners

Posted by


React JS is a popular JavaScript library that is commonly used for building user interfaces. One of the key features of React is its use of Higher Order Components (HOC), which are a powerful tool for code reusability and component composition. In this tutorial, we will cover the basics of Higher Order Components in React JS in just 20 minutes.

What is a Higher Order Component (HOC)?
In React JS, a Higher Order Component is a function that takes a component and returns a new component. HOCs are used to add functionality to existing components without modifying their code. This allows for code reusability and a clean separation of concerns in your application.

Why use Higher Order Components?
There are several benefits to using Higher Order Components in React JS. Some of the main advantages include:

  • Reusability: HOCs allow you to create reusable pieces of logic that can be applied to multiple components in your application.
  • Composition: HOCs enable you to compose components with different functionalities, making it easy to build complex UIs.
  • Separation of Concerns: HOCs help to keep your components focused on their own responsibilities, making your code more modular and maintainable.

Creating a Higher Order Component in React JS
Now that you understand the basics of Higher Order Components, let’s walk through an example of creating a simple HOC in React.

Step 1: Create a new React component
First, create a new React component that you want to enhance with the HOC. For this example, let’s create a simple Button component:

import React from 'react';

const Button = ({ text }) => {
  return (
    <button>{text}</button>
  );
};

export default Button;

Step 2: Create a Higher Order Component
Next, create a Higher Order Component that will add some additional functionality to the Button component. In this example, we will create a HOC called withColor that will add a color prop to the component:

import React from 'react';

const withColor = (WrappedComponent, color) => {
  return (props) => {
    return <WrappedComponent {...props} style={{ color }} />;
  };
};

export default withColor;

Step 3: Enhance the Button component with the HOC
Finally, enhance the Button component with the withColor HOC by wrapping it with the HOC function and passing the desired color as an argument:

import React from 'react';
import withColor from './withColor';

const Button = ({ text }) => {
  return (
    <button>{text}</button>
  );
};

const ColoredButton = withColor(Button, 'red');

export default ColoredButton;

Now, when you use the ColoredButton component in your application, it will have the specified color applied to it.

Conclusion
In this tutorial, we covered the basics of Higher Order Components in React JS. We discussed what HOCs are, why they are useful, and how to create a simple HOC in just 20 minutes. By using HOCs, you can create reusable and composable components that help you build more efficient and maintainable applications. I hope this tutorial has been helpful in introducing you to Higher Order Components in React JS. Happy coding!

0 0 votes
Article Rating

Leave a Reply

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ajayt2054
20 days ago

Thanks🎉

@tamizhchemmal5400
20 days ago

Thank you for your explanation.. Wonderful way of teaching.

@FrontendPower
20 days ago

This is best example on HOC not only for beginners bust also for experienced professionals.
Thanks for sharing your valuable knowledge with all of us.
Kindly concentrate on Wrapper and compound components also and share best and useful examples on these two topics.
Thanks in advance.

@AdrianoGrataniClassicalGuitar
20 days ago

congrats for this perfect tutorial, I was struggling with hoc in react…

@iranna9065
20 days ago

Well explained. Thank you

@Cristianxf1
20 days ago

Very complete and well explained. Thank you!

@mahendraumare4483
20 days ago

why you take NewComponent() Function…

7
0
Would love your thoughts, please comment.x
()
x