The Wonders of Composition with React HOCs
Hello, young grasshoppers! I'm bringing a bit more content about React. I believe it will be very helpful, especially for beginners. Today, I'll explain a little about HOC (Higher-Order Component).

What are Higher-Order Components?
In summary, an HOC is a function that receives a component and returns another component. 😕 They are like HOFs (Higher-Order Functions), which is a function that receives another function and returns a function (now it's getting worse) hehehe.
I know, it may seem strange, but it will make sense.
HOCs are very common in libraries like Redux (connect), Relay (createFragmentContainer), Mobx (observer), and others. It's also possible to create your own HOC (of course 🍭).
Note: HOCs are not part of React itself — they are a pattern for development using composition and inheritance.
The main purpose of HOCs is the sharing of common functionality between components without code duplication, basically.
With this, it's possible to create a component that has an internal state to control its "children".
Imagine a situation where we have a page that receives news and has several components. Communication between them is done via props. Each component has its own rendering and behavior, and they depend on the parent component to display content. But while the parent's data hasn't finished loading, the children cannot be shown. You could add a loader to each component until the data finishes loading. That will work, but it can become a maintenance problem, not to mention code duplication. It won't look good, so it's definitely not what we want.
What can I do with HOCs?
- Code reuse, logic, and abstraction;
- Render control;
- State abstraction and manipulation;
- Props manipulation.
There are two types of HOCs:
Proxy
These are HOCs that pass properties to their children. This is also the type I used to create the example in this article.
function ppHOC(WrappedComponent) {
return class PP extends React.Component {
render() {
const newProps = {
user: currentLoggedInUser
}
return <WrappedComponent {...this.props} {...newProps}/>
}
}
}
When should I use a proxy?
- Props manipulation
- Accessing the instance via ref
- State abstraction
- Wrapping a component with other elements
Inheritance Inversion
This type of HOC extends its class from the component it receives, thus having access to the instance, state, lifecycle, hooks, and render props.
function iiHOC(WrappedComponent) {
return class Enhancer extends WrappedComponent {
render() {
return super.render()
}
}
}
When should I use Inheritance Inversion?
- Render Hijacking
- State manipulation
Now let's create our HOC =D
I used create-react-app to make project initialization easier.
I won't go into detail about how create-react-app works and when to use it — that's a topic for another article. But the installation and usage are very straightforward, as we'll see below.
If you have npm 5.2+ you can install it like this:
npx create-react-app hoc-react-example
If not, you can install it as follows:
npm i -g create-react-app && create-react-app hoc-react-example
After installation, run the project:
With NPM:
npm start
or with YARN:
yarn start
Once done, a browser tab will open. http://localhost:3000/
Well, since I'm a beer enthusiast, I'll create an HOC that "brews beer". For this, I kept the project as simple as possible with just a few files, and the structure looks like this:
hoc-react-example
├── README.md
├── node_modules
├── package.json
