🌐
React
legacy.reactjs.org › docs › higher-order-components.html
Higher-Order Components – React
In those rare cases where you need to apply a HOC dynamically, you can also do it inside a component’s lifecycle methods or its constructor. Sometimes it’s useful to define a static method on a React component. For example, Relay containers expose a static method getFragment to facilitate ...
🌐
W3Schools
w3schools.com › react › › react_hoc.asp
React Higher Order Components
Note: HOCs are functions that take a component and return an enhanced version of that component. To demonstrate how HOCs work, let's create a simple example - adding a border to any component:
Discussions

Understanding React Higher-Order Components
According to the documentation, ... 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 such as this, which ... More on stackoverflow.com
🌐 stackoverflow.com
HOC(Higher-order components) are not commonly used in modern React code. (?)
What do you do when you need not ... prop for example? ... I'm a big believer that there are not absolutes in coding. I'm sure there's some edge case out there than an HOC can do that others can't, although I cannot personally think of one. Nevertheless, HOCs and render props have both been superceded by hooks, which are a far more versatile solution. ... That’s interesting! I just used added a render prop to a Form component I created for a small library. Our library leverages react-hook-form, ... More on reddit.com
🌐 r/reactjs
63
66
October 23, 2023
Higher Order Components: Are you still using them since Hooks came along?
They're not completely legacy, but the number of use cases for them dropped dramatically after hooks came out. See my post and talk here for thoughts on the differences: Thoughts on React Hooks, Redux, and Separation of Concerns ReactBoston 2019: Hooks, HOCs, and Tradeoffs Also, Not All Components Are Created Equal is a great read with insights on how to structure components. More on reddit.com
🌐 r/reactjs
71
144
July 19, 2021
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
🌐
React TypeScript Cheatsheets
react-typescript-cheatsheet.netlify.app › full hoc example
Full HOC Example | React TypeScript Cheatsheets
The actual HoC. export function withTheme<T extends WithThemeProps = WithThemeProps>( WrappedComponent: React.ComponentType<T> ) { // Try to create a nice displayName for React Dev Tools. const displayName = WrappedComponent.displayName || ...
🌐
Patterns.dev
patterns.dev › react › hoc-pattern
HOC Pattern
Instead of wrapping the DogImages component with the withHover HOC, we can use the useHover hook right inside the DogImages component. ... Perfect! Instead of wrapping the DogImages component with the withHover component, we can simply use the useHover hook within the component directly. Generally speaking, React Hooks don’t replace the HOC pattern.
🌐
freeCodeCamp
freecodecamp.org › news › higher-order-components-in-react
How to Use Higher-Order Components in React
July 28, 2023 - Additionally, each time you add or complete a task, the app will log the events in the browser console using the withLogger HOC. For example, the console will show: That's it! You have now implemented a real-world example of using the withLogger Higher-Order Component in a React project.
🌐
LogRocket
blog.logrocket.com › home › how to use react higher-order components
How to use React higher-order components - LogRocket Blog
February 21, 2025 - For a broader perspective on advanced React logic reuse, see “The modern guide to React state patterns.” · If your HOC involves expensive computations, consider performance optimization techniques like memoization to prevent unnecessary re-renders. Below is an example using useMemo and ...
🌐
Flexiple
flexiple.com › react › introduction-to-higher-order-components-in-react-by-example
Introduction to Higher-Order Components in React by example - Flexiple
Libraries such as Redux’s connect, react-router’s withRouter are good examples of why we should think about implementing higher order components. We use higher order components to primarily reuse logic in React apps. However, they have to render some UI. Hence, HOCs are inconvenient when you want to share some non-visual logic.
🌐
Medium
medium.com › @jeffersonscjunior › higher-order-components-hocs-in-reactjs-8ffb34dd2820
Higher Order Components (HOCs) in ReactJS | by Jefferson Soares Costa Junior | Medium
January 2, 2025 - In this article, we’ll talk about a very useful design pattern in reactJS projects. ... Higher-order components (HOCs) are an advanced standard in ReactJS that involves creating components that take other components as arguments and return new components.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › react-js-higher-order-components
ReactJS Higher-Order Components - GeeksforGeeks
... import React from 'react'; ... const withName = (OriginalComponent) => { const NewComponent = (props) => { return <OriginalComponent {...props} name="GeeksforGeeks" />; }; return NewComponent; }; export default withName;...
Published   January 21, 2026
🌐
Smashing Magazine
smashingmagazine.com › 2020 › 06 › higher-order-components-react
Higher-Order Components In React — Smashing Magazine
Here are some examples of real-world HOCs you might have come across: ... It is a component. It takes another component as an argument. Then, it returns a new component. The component it returns can render the original component that was passed to it. The snippet below shows how a HOC is structured in React:
🌐
Robin Wieruch
robinwieruch.de › react-higher-order-components
React Higher-Order Components (HOCs) - Robin Wieruch
April 19, 2022 - Without going into further detail about HOFs in JavaScript here, let’s walk through this whole concept when speaking about HOCs in React. There we will walk through normal functions, functions that take other functions (function components) as arguments, and functions that are composed into each other as you have seen in the last code snippet. Higher-Order Components take any React component as input component and return an enhanced version of it as output component. 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 (TodoList), because none of them want to be bothered by them.
🌐
Jakoblind
blog.jakoblind.no › simple-explanation-of-higher-order-components-hoc
Simple explanation of Higher-Order Components (HOC) | blog.jakoblind.no
Let’s look at an example of the most simple HOC possible. // Take in a component as argument WrappedComponent function simpleHOC(WrappedComponent) { // And return a new anonymous component return class extends React.Component { render() { return <WrappedComponent {...this.props} /> } } }
🌐
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 - But when you develop a React application it is not always straightforward to apply the DRY principles and you have to write repeated code in multiple components. But by using HOCs you can reduce the repeated code, by encapsulating that logic into a Higher Order Component. We will look at some examples and patterns below.
🌐
CodinGame
codingame.com › playgrounds › 8595 › reactjs-higher-order-components-tutorial
Coding Games and Programming Challenges to Code Better
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.
🌐
HackerNoon
hackernoon.com › mastering-higher-order-components-hocs-in-react
Mastering Higher Order Components (HOCs) in React | HackerNoon
October 13, 2023 - programming#react#hoc#react-hoc#higher-order-components#frontend-development#javascript-frameworks#react-components#react-development
🌐
Hands on React
handsonreact.com › higher-order components
Higher-Order Components | Hands on React
```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 />); To better understand when to use HOCs it is helpful to compare them to using composition with components.
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.

🌐
Medium
soorajchandran.medium.com › introduction-to-higher-order-components-hoc-in-react-383c9343a3aa
Introduction to higher-order components (HOC) in React | by Sooraj Chandran | Medium
May 10, 2020 - A classic example of functional programming example is the multiplication: const multiply = (x) => (y) => x * ymultiply(5)(20) Similarly, a HOC takes another component as argument.
🌐
Medium
medium.com › @sankalpa115 › higher-order-components-for-data-fetching-in-react-9b71fd1a9436
Higher Order Components for data fetching in React | by Sankalpa Neupane | Medium
May 20, 2024 - By abstracting data fetching into reusable HOCs, developers can create cleaner, more maintainable codebases and improve overall application performance. import React, { useState, useEffect } from 'react'; // Define the higher-order component const withDataFetching = (WrappedComponent, dataSource) => { return () => { const [data, setData] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { const fetchData = async () => { setIsLoading(true); try { const response = await fetch(dataSource); if (!response.ok) { throw new Error('Failed to fetch data'); } const result = await response.json(); setData(result); } catch (error) { setError(error); } finally { setIsLoading(false); } }; fetchData(); }, []); return ( <WrappedComponent data={data} isLoading={isLoading} error={error} /> ); }; };