ReactJS

ReactJS | Higher-Order Components

Hello React Friends,

In this article, we will go through the ReactJS HOC concept, why to use them, and how to use them in ReactJS with an example.

So let’s get started with ReactJS Higher-Order Components.

What are Higher-Order Components or HOC in React?

Higher-order components or HOC is the advanced method of reusing the component functionality logic in the project. It simply takes the original component and returns the enhanced component. HOC uses the DRY (don’t-repeat-yourself) programming principle, which is very important while building an application or writing code.

Reason to use Higher-Order Components in React:

  • Easy to handle
  • Get rid of copying the same logic in every component
  • Makes code optimized and more readable

Syntax of Higher-Order Components in React:

const EnhancedComponent = higherOrderComponent(OriginalComponent);

Example of Higher-Order Components in React:

import React, { useState } from 'react'
const HOC = (OriginalComponent)=>{
    function NewComponent () {
        const [Value, setValue] = useState(0)
        const handleClick = () =>{
            console.log(2)
            setValue(Value + 2)
        }
        return <OriginalComponent handleClick={handleClick} Value={Value} />
    }
    return NewComponent
}
export default HOC
import React, { useState } from 'react'
import HOC from './HOC'
const Person1 = ({Value, handleClick}) => {
    return (
        <>
            <div>Person1: {Value}</div>
            <button onClick={()=> handleClick()}> Count number </button>
        </>
   )
}
export default HOC(Person1)
import React from 'react'
import HOC from './HOC'
const Person2 = ({handleClick, Value}) => {
    return (
        <>
            <div>Person2 : {Value}</div>
            <button onClick={()=> handleClick()}> Count number </button>
        </>
    )
}
export default HOC(Person2)

Conclusion:

Hence, this was all about Higher-Order Components in ReactJS. If you face any difficulty, kindly share it with me through the comment box. Connect with ReactJs Developers and learn more.

Spread the tutorial with your friends to help them learn about Higher Order Components in ReactJS.

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

16 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago