A tale of Higher-Order Components & Render Props
Tags: react, javascript, renderprops, hoc
Published: 2018-02-03 15:14:50

---
title: "A tale of Higher-Order Components & Render Props"
published: true
tags: react,javascript,render-props,HOC
canonical_url: https://medium.com/ingenious/a-tale-of-higher-order-components-render-props-a1ba47e8cfeb
---

In the past few months there has been an endless debate in the react community on [Higher-Order Components](https://reactjs.org/docs/higher-order-components.html) vs. [Render Props](https://reactjs.org/docs/render-props.html) as techniques for sharing concerns and reusing common logic among components. There are tons of tutorials and courses explaining their benefits and drawbacks. Just in case you haven’t seen any of those resources, head over to the React docs which have a short and focused introduction to both&nbsp;terms.

Here at [Ingenious](http://ingenious.agency/) we have our fair share of React Projects, of all sizes, among various teams. So far we’ve been using Higher-Order Components (HOC for short) mainly through libraries like [recompose](https://github.com/acdlite/recompose) for implementing our own “Reuse this logic” component or as direct clients of popular libraries like [react-redux](https://github.com/reactjs/react-redux) whose main features are exposed via HOC. Just a few days ago we had the opportunity of “joining” the debate and choose one of the two approaches to implementing a new feature. The story goes like&nbsp;this.

We built a product where users need to leave recommendations to other users, and each recommendation can have a list of comments. Think of “Judges” that provide advice to “Participants” of a contest where anyone, be it a Judge or Participant, can give feedback multiple times on a given Recommendation.

We went all in and proposed a beautiful UI that looks similar to&nbsp;[this](https://github.com/pepesenaris/hoc-and-render-props-example/tree/create-only):

![](https://cdn-images-1.medium.com/max/484/1*bgQPgyboVVnvqh1VVBKWvA.png)<figcaption>Recommendations with nested comments</figcaption>

Everything went smoothly, client was happy and end of blog&nbsp;post.


* * *


Actually, at some point, the client requested that both Recommendations and their Comments should be editable within the first 10 minutes of being created. The final intended set of actions a user can perform with Recommendations and Comments were quite different, but the Edit workflow was pretty much the same for both entities. We wanted to make the edition similar to what users already know, which meant reusing as much as possible the UI we already had. The goal now was to add a way of selecting a recommendation or comment, fill the same input used to create it, and save the modification.

We started with Comments and built a HOC to allow editing them. Fast forwarding in time, after refactoring the common bits, we ended up with a component that allowed us to use the same logic to create/edit both Recommendations and Comments. Something like&nbsp;[this](https://github.com/pepesenaris/hoc-and-render-props-example/tree/edit-hoc):

![](https://cdn-images-1.medium.com/max/1024/1*Ag10snUKWqjglH3N_lPBaQ.png)

By wrapping the Recommendations and Comments listings with editRecentEntity, we only need to toggle the edition mode in both entities and problem solved&nbsp;:-)

![](https://cdn-images-1.medium.com/max/958/1*1W4pUkKMoqAbWscCRP-sPw.png)<figcaption>The Edit/Create links toggle the Edition mode for either Recommendations or Comments</figcaption>


* * *


A few days later another client request arrived. Now we needed to show _“archived”_ Recommendations, which are read-only entries with more than three months old, and present a _Loading_ indicator instead of the input form while the more recent _“active”_ Recommendations are being retrieved from the&nbsp;server.

Up to this point we simply rendered a list of Recommendations followed by a Form component, all wrapped up with the editRecentEntity HOC. The Comments listing used the same pattern as&nbsp;well.

![](https://cdn-images-1.medium.com/max/1024/1*vOjJwgzlVqmJp8QtzfSZKw.png)

Hiding the forms for the archived entries has a straightforward solution, the problem then was that adding more code to show a Loading Spinner, instead of the Form, seems a bit clumsy given that it was only necessary for one type of Recommendations.

A clean solution here, was to move the responsibility of when and how to show the form, to the parent component by using the special React children prop. This idea combined with the Render Props pattern allowed to pass the necessary props to the form, so we can continue to support the creation and edition of Recommendations.

![](https://cdn-images-1.medium.com/max/1024/1*9TpV39GzU2jc0OWqT-P3HA.png)<figcaption>Render Props to the rescue…</figcaption>

Here the Render Props pattern provides an explicit API for communication between Parent and Children components. All three properties, passed to the form, depends on the logic already implemented in editRecentEntity and at the same time the _“marvelous”_ Loading component can be used only where and how it’s needed. Go and play with the final [code](https://github.com/pepesenaris/hoc-and-render-props-example) if that’s your&nbsp;thing.

The main lesson here, at least the one we learned building this feature, is that what matters most is solving the problem at hand without taking sides on hype-oriented battles of which pattern is better than the other. If the client would have stopped with the Create/Edit functionality, our code will be as worthy as the final version. Be it Higher-Order Components or Render Props, always try to choose whatever pattern, tool, library helps you solve the problem as clear as possible.

That’s all folks… Happy&nbsp;Coding.


* * *


_PS: If you liked, or even better, disliked what you read here please_ [_drops us a line_](http://ingenious.agency/contact/)_. We love to talk about technology and are always looking for awesome people that enjoy learning with and teaching us how to solve people problems._