Higher order components in React.js

Higher order component in short called as hoc. It’s a pattern which receives a component and returns a new component with add-on features to it.

//hoc is the name of a custom JavaScript function

const AddOnComponent= hoc(SimpleComponent);

We use component with state and/or props to build an UI. Similar way a hoc builds a new component from the provided component.

Use of hoc is making a cross cutting concerns in React. The components will take care of individual responsibility of single tasks while hoc functions will take care of cross cutting concerns.

Connect function from redux is an example of hoc.

A practical example of hoc

Display welcome message to customer or admin based on user type.

Class App extends React.Component{
   render(){
      return (
         <UserWelcome message={this.props.message} userType={this.props.userType} />
      );
   }
}
Class UserWelcome extends React.Component{
   render(){
      return(
         <div>Welcome {this.props.message}</div>
      );
   }
}
const userSpecificMessage=(WrappedComponent)=>{
   return class extends React.Component{
      render(){
         if(this.props.userType==’customer’){
         return(
            <WrappedComponent {…this.props}/>
         );
         } else {
            <div> Welcome Admin </div>
         }
      }
   }
}

export default userSpecificMessage(UserWelcome)

In the UserWelcome, we are just displaying message to user passed by parent component App.js.

The UserComponent is wrapped by hoc userSpecificMessage which received the props from wrapped component i.e. UserComponent

The hoc userSpecificMessage decides which message to display based on the type of user.

If the type of the user is customer it displays the message as it is. But if the user is not customer then it displays a Welcome Admin message by default.

With this way we can add the common functionality required by components in hoc and use it whenever required.

It allows code reuse and keeps the components clean with the individual tasks only.

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…