×

React Higher-Order Components

React Higher-Order Components

In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new component. It is not the feature of the React API, but a pattern which emerges from React compositional nature. These are similar to the functions of JavaScript that are used to add the additional functionalities in the existing component.

The HOC function accepts the other function in the form of an argument. The main goal of it to decompose the component logic into smaller and simpler functions, which can be reused in the future.

Syntax:

const NewComponent = higherOrderComponent(WrappedComponent);

As we know that, the component transforms the props into UI, and the HOC converts the component into another component and also allows us to add the additional data or functionality into it. Higher-order components are common in the third-party libraries. Some of the examples of the HOCs are Redux’s connect and Relay’s createFragmentContainer.

Let’s understand the working of HOCs by using the following example:

//Function Creation  
 function mul (x, y) {  
   return x * y  
 }  
 function higherOrder(x, addReference) {  
   return addReference(x, 20)  
 }  
 //Function call  
 higherOrder(30, mul) // 600   

In this example, there are two functions mul() and higherOrder(). Now, we are also giving the mul() function as an argument to the higherOrder() function.

The function gets passed, is called a callback function, and the function where you have passed the callback function is called as higher-order (HOCs) function.

Example:

Now, create a new file HOC.js, and make another function HOC in it. It will accept one argument as a component.

HOC.js

import React, {Component} from 'react'; 
export default function Hoc(HocComponent){ 
return class extends Component{ 
render(){ 
return ( 
); } } }

Include the HOC.js file into your App.js file. In this file, we have to call the HOC function.  

App = Hoc(App); 

App.js

import React, { Component } from 'react'; 
import Hoc from './HOC'; 
class App extends Component { 
render() { 
return ( 

Example of Higher Order Components

Hello World :) :)

Welcome to the tutorialandexample.com

) } } App = Hoc(App); export default App;

Output:

After the execution of the above code, the output will be:

call the HOC function.

Higher-Order Component Conventions

  • It is not recommended to use the HOCs within the render method of the component.
  • The static methods must have copied over for their accessing. It can be done by using a hoist-non-react-statics package for automatically copying all the non-react static methods.
  • If you want to add the ref to the element within the HOC component, then the ref refers to the instance of the outermost container component, instead of the wrapper component.

Related Topics

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

ReactJS Tutorial

ReactJS Introduction React Tutorial helps you to understand the basic and some advanced concepts of ReactJS. As of now, it is the essential front-end library that is developed by a...

3 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

React Fragments

React Fragments The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM. In React,...

2 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.

React Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

7 minutes read.

React Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.

React Bootstrap

React Bootstrap React has a widely used JavaScript framework for creating web applications, and Bootstrap has become the most popular CSS framework. Single-page apps have become popular from the last few...

9 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

2 minutes read.

How to use React Context

React Context React Context is used to pass the data through the component tree without passing the props down manually at the every level. In React Application, the data is passed in a top-down approach...

4 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.