A HOC is a function that takes a Component as one of its parameters and enhances that component in some way.

If HOCs create a new enhanced component, can it be possible not to pass in any component as argument at all?

Nope, then it wouldn't be a HOC, because one of the conditions is that they take a component as one of the arguments and they return a new Component that has some added functionality.

In an example such as this, which is the higher order component, the Button or the EnhancedButton.

EnhanceButton is the HOC and FinalButton is the enhanced component.

I tried creating one HOC like this: ... Running this does not show the HOC, only the div

That's because your createSetup function is not a HOC... It's a function that returns a component, yes, but it does not take a component as an argument in order to enhance it.

Let's see an example of a basic HOC:

const renderWhen = (condition, Component) =>
  props => condition(props)
    ? <Component {...props} />
    : null
);

And you could use it like this:

const EnhancedLink = renderWhen(({invisible}) => !invisible, 'a');

Now your EnhancedLink will be like a a component but if you pass the property invisible set to true it won't render... So we have enhanced the default behaviour of the a component and you could do that with any other component.

In many cases HOC functions are curried and the Component arg goes last... Like this:

const renderWhen = condition => Component =>
  props => condition(props)
    ? <Component {...props} />
    : null
);

Like the connect function of react-redux... That makes composition easier. Have a look at recompose.

Answer from Josep on Stack Overflow
๐ŸŒ
React
legacy.reactjs.org โ€บ docs โ€บ higher-order-components.html
Higher-Order Components โ€“ React
The container components created by HOCs show up in the React Developer Tools like any other component. To ease debugging, choose a display name that communicates that itโ€™s the result of a HOC. The most common technique is to wrap the display name of the wrapped component. So if your higher-order component is named withSubscription, and the wrapped componentโ€™s display name is CommentList, use the display name WithSubscription(CommentList):
๐ŸŒ
W3Schools
w3schools.com โ€บ react โ€บ โ€บ react_hoc.asp
React Higher Order Components
Think of it like putting a case on your phone - the case adds new features (like water protection) without changing the phone itself. Note: HOCs are functions that take a component and return an enhanced version of that component.
Discussions

Understanding React Higher-Order Components
Can someone please explain Higher-order components in React. I have read and re-read the documentation but cannot seem to get a better understanding. According to the documentation, HOCs help remove duplication by creating a primary function that returns a react component, by passing arguments to that function. I have a few questions on that. If HOCs create a new enhanced component, can it be possible not to pass in any component as argument at all? In an example ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
HOC(Higher-order components) are not commonly used in modern React code. (?)
Check this video by the creator ... write a higher order component Since HOC and hooks try to solve the same problem, You rarely will find a need to use HOC in modern react. ... Dogmatic click bait. Still valid use cases for HOCs when you need to affect rendering of a component or modify props. ... Can you please give me an example of such a ... More on reddit.com
๐ŸŒ r/reactjs
63
66
October 23, 2023
Are HOCs (Higher Order Components) still a thing?
Generally withBlah() HOCs have been replaced with useBlah() hook calls. Obviously the pattern is still valid (see React.memo) and can be used, but no surprise that hooks have become the defacto replacement. More on reddit.com
๐ŸŒ r/reactjs
82
70
May 16, 2024
Are HOC's (Higher-Order Components) heavily discouraged by the React Team moving forward? They are missing from the Beta React.js docs. Curious if there are plans to re-include them.
I don't think they're discouraged but there are better patterns now that functional components exist. Since FC became a thing I have yet to run into a scenario where I need a HoC. More on reddit.com
๐ŸŒ r/reactjs
17
21
January 27, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ reactjs โ€บ react-js-higher-order-components
ReactJS Higher-Order Components - GeeksforGeeks
A Higher-Order Component takes a component as input. It returns a new component with added functionality. The new component behaves like the original but with extra features. Create a React application by using the following command. ... After creating your project folder i.e. foldername, move to it using the following command. ... Example 1: Let say, we need to reuse the same logic, like passing on the name to every component.
Published ย  January 21, 2026
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ higher-order-components-in-react
How to Use Higher-Order Components in React
July 28, 2023 - That's it! You have now implemented a real-world example of using the withLogger Higher-Order Component in a React project.
๐ŸŒ
Flexiple
flexiple.com โ€บ react โ€บ introduction-to-higher-order-components-in-react-by-example
Introduction to Higher-Order Components in React by example - Flexiple
See the Pen React Hoc by Mohan Dere (@mohandere) on CodePen. Debugging HOCs can be hard as we can have multiple instances on the same HOC on the same page. To tackle this, we can choose a better display name which communicates that it is the result of a HOC. For example, if your higher-order component is named WithSearch, and the wrapped componentโ€™s display name is ProductsList, you should use the display name WithSearch(ProductsList) as shown below:
๐ŸŒ
CSS-Tricks
css-tricks.com โ€บ what-are-higher-order-components-in-react
What are Higher-Order Components in React? | CSS-Tricks
April 19, 2018 - Thanks to the idea of higher order components, we can create a search container component and wrap it around other components. Letโ€™s call the component withSearch. This component will render the input field for our search and also manage our ...
๐ŸŒ
Patterns.dev
patterns.dev โ€บ react โ€บ hoc-pattern
HOC Pattern
A good example of this is Apollo Client. No experience with Apollo Client is needed to understand this example. One way to use Apollo Client is through the graphql() higher order component.
Find elsewhere
๐ŸŒ
Hands on React
handsonreact.com โ€บ higher-order components
Higher-Order Components | Hands on React
The `wrapper` function is the higher-order function or more specificlaly component in the code below. ```js function Inner(props) { return <span>Inner</span>; } function wrapper(Inner, value) { function Outer(props) { return ( <div> <h3>Outer</h3> <Inner {...props} /> <em>{value}</em> </div> ); } return Outer; } const Outer = wrapper(Inner, 'Peace'); function App() { return <Outer />; } ReactDOM.createRoot(document.getElementById("root")).render(<App />);
๐ŸŒ
Smashing Magazine
smashingmagazine.com โ€บ 2020 โ€บ 06 โ€บ higher-order-components-react
Higher-Order Components In React โ€” Smashing Magazine
The snippet below shows how a HOC ... argument WrappedComponent const higherOrderComponent = (WrappedComponent) => { // And return another component class HOC extends React.Component { render() { return <WrappedComponent />; } } return HOC; };...
Top answer
1 of 5
11

A HOC is a function that takes a Component as one of its parameters and enhances that component in some way.

If HOCs create a new enhanced component, can it be possible not to pass in any component as argument at all?

Nope, then it wouldn't be a HOC, because one of the conditions is that they take a component as one of the arguments and they return a new Component that has some added functionality.

In an example such as this, which is the higher order component, the Button or the EnhancedButton.

EnhanceButton is the HOC and FinalButton is the enhanced component.

I tried creating one HOC like this: ... Running this does not show the HOC, only the div

That's because your createSetup function is not a HOC... It's a function that returns a component, yes, but it does not take a component as an argument in order to enhance it.

Let's see an example of a basic HOC:

const renderWhen = (condition, Component) =>
  props => condition(props)
    ? <Component {...props} />
    : null
);

And you could use it like this:

const EnhancedLink = renderWhen(({invisible}) => !invisible, 'a');

Now your EnhancedLink will be like a a component but if you pass the property invisible set to true it won't render... So we have enhanced the default behaviour of the a component and you could do that with any other component.

In many cases HOC functions are curried and the Component arg goes last... Like this:

const renderWhen = condition => Component =>
  props => condition(props)
    ? <Component {...props} />
    : null
);

Like the connect function of react-redux... That makes composition easier. Have a look at recompose.

2 of 5
4

In short, If you assume functions are analogues to Components, Closure is analogous to HOC.

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ react-higher-order-components
A Quick Intro to React's Higher-Order Components | DigitalOcean
May 12, 2018 - Our withUser function takes a component as an argument and returns a higher order component. Three months from now, if we decide to change things around, we only have to edit our HOC. If you werenโ€™t familiar with HOCs before, you might have encountered them without realizing it! Some notable examples: react-redux: connect(mapStateToProps, mapDispatchToProps)(UserPage)
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ reactjs โ€บ reactjs_higher_order_components.htm
ReactJS - Higher Order Components
Let us take a look at a simple example to easily understand how this concept works. The MyHOC is a higher order function that is used only to pass data to MyComponent. This function takes MyComponent, enhances it with newData and returns the enhanced component that will be rendered on the screen. ...
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to use react higher-order components
How to use React higher-order components - LogRocket Blog
February 21, 2025 - Overall, while both approaches manage state similarly, the HOC is ideal for wrapping and enhancing an existing component without directly modifying it, whereas a custom Hook offers a cleaner solution for sharing stateful logic across multiple components without adding an extra layer. According to Reactโ€™s documentation, a typical React HOC has the following definition: โ€œA higher-order component is a function that takes in a component and returns a new component.โ€
๐ŸŒ
DhiWise
dhiwise.com โ€บ post โ€บ how-react-higher-order-components-improve-code-reusability
How to Implement React Higher Order Components in Your Project
January 27, 2024 - Now that we've seen how to write a higher order component, let's look at a more practical example. Let's say we have an application that fetches data from an API. We have multiple components that need to fetch data, and we want to avoid duplicating the fetch logic in each component. This is a perfect use case for a higher order component! First, let's create a withData HOC that fetches data and passes it as a prop to the wrapped component: ... 1 import React, { useEffect, useState } from 'react'; 2 3 function withData(WrappedComponent, fetchData) { 4 return function EnhancedComponent(props) { 5 const [data, setData] = useState(null); 6 7 useEffect(() => { 8 fetchData().then(setData); 9 }, [fetchData]); 10 11 return <WrappedComponent {...props} data={data} />; 12 }; 13 } 14 15 export default withData; 16
๐ŸŒ
Developerway
developerway.com โ€บ posts โ€บ higher-order-components-in-react-hooks-era
Higher-Order Components in React Hooks era
February 27, 2022 - For example, we can send those logging events when a component is mounted: export const withLoggingOnMount = <TProps extends unknown>(Component: ComponentType<TProps>) => { ... And exactly the same story as with onClick for adding data via arguments ...
๐ŸŒ
CodinGame
codingame.com โ€บ playgrounds โ€บ 8595 โ€บ reactjs-higher-order-components-tutorial
ReactJS Higher Order Components Tutorial
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
๐ŸŒ
Reddit
reddit.com โ€บ r/reactjs โ€บ hoc(higher-order components) are not commonly used in modern react code. (?)
r/reactjs on Reddit: HOC(Higher-order components) are not commonly used in modern React code. (?)
October 23, 2023 - Personally I think common patterns in a given codebase should be leaned into to reduce cognitive overhead for readers of the code, and since hooks are used for most everything, APIs are going to be easier to understand as hooks now, which is why we see a lot of popular libraries and frameworks in the react world embracing hooks and not using as many HOCs. ... That's not an HOC. Those are just children props / render props. ... Higher-Order Human: a human that takes a penny and leaves a penny. ... It's not "and". It's "or". ... Trying to better understand the difference and terminology! ... How would you solve the problem of redundancy if you had two components that are somewhat similar, returning different JSX, and sharing the same logic except for one function?
๐ŸŒ
Dead Simple Chat
deadsimplechat.com โ€บ blog โ€บ higher-order-componets-in-react
Higher Order Components in React: Examples with Common Use-case Patterns
October 28, 2023 - Higher Order Components can also be used to handle un-handeled errors using Error Boundaries. Once caveat is that the Error Boundary is not supported in functional components. Hence, only the HOC has to be the class componenet, the WrappedComponent can be a functional component. Let's check out an example of Error Boundary using and HOC: import React, { Component } from "react"; function withErrorBoundary(WrappedComponent) { return class ErrorBoundary extends Component { constructor(props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error) {
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ higher-order components: a react application design pattern
Higher-order Components: A React Application Design Pattern
November 11, 2024 - It allows for the reusability of ... for Higher Order Components? Common use cases for HOCs include authentication, data fetching, logging, and code reuse. For example, an ......
๐ŸŒ
Robin Wieruch
robinwieruch.de โ€บ react-higher-order-components
React Higher-Order Components (HOCs) - Robin Wieruch
April 19, 2022 - Higher-Order Components take any ... In our example, the goal would be to shield away specifically all the conditional rendering edge cases right in between of parent component (App) and child component ......