×

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 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.

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 State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

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 Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 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 Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.

React CSS

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 minutes read.

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.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

3 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

ReactJS vs AngularJS

Difference between ReactJS and AngularJS AngularJS AngularJS is the JavaScript framework that is open source, and it is also used to build a dynamic web application. It is developed in 2009 by...

5 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 Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

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.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

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.

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.