Endless possibilities of composing and reusing functionality

Alex Moldovan
Feb 4, 2018 · 5 min read

Let’s take a closer look at some of the patterns that are emerging in the React ecosystem. These patterns improve readability, code clarity, and push your code towards composition and reusability.

I started working with React about 3 years ago. At that time, there were no established practices from which to learn in order to leverage its capabilities.

It took about two years for the community to settle around a few ideas. We shifted from React.createClass to the ES6 class and pure functional components. We dropped mixins and we simplified our APIs.

Now as the community is larger than ever, we’re starting to see a couple of nice patterns evolving.

In order to understand these patterns, you need a basic understanding of the React concepts and its ecosystem. Please note, however, that I will not cover them in this article.

Let’s begin!


Conditional Render

I’ve seen the following scenario in a lot of projects.

When people think of React and JSX, they still think in terms of HTML and JavaScript.

So the natural step is to separate the conditional logic from the actual return code.

This tends to get out of control, with multiple ternaries at the beginning of each render function. You constantly have to jump inside the function to understand when a certain element is rendered or not.

As an alternative, try the following pattern, where you benefit from the execution model of the language.

If condition is false, the second operand of the && operator is not evaluated. If it is true, the second operand — or the JSX we wish to render — is returned.

This allows us to mix UI logic with the actual UI elements in a declarative way!

Treat JSX like it’s an integral part of your code! After all, it’s just JavaScript.


Passing Down Props

When your application grows, you have smaller components that act as containers for other components.

As this happens, you need to pass down a good chunk of props through a component. The component doesn’t need them, but its children do.

A good way of bypassing this is to use props destructuring together with JSX spread, as you can see here:

So now, you can change the props needed for Details and be sure that those props are not referenced in multiple components.


Destructuring Props

An app changes over time, and so do your components. A component you wrote two years ago might be stateful, but now it can be transformed into a stateless one. The other way around also happens a lot of times!

Since we talked about props destructuring, here’s a good trick I use to make my life easier in the long run. You can destructure your props in a similar manner for both types of components, as you can see below:

Notice that lines 2–4 and 11–13 are identical. Transforming components is much easier using this pattern. Also, you limit the usage of this inside the component.


Provider Pattern

We looked at an example where props need to be sent down through another component. But what if you have to send it down 15 components?

Enter React Context!

This is not necessarily the most recommended feature of React, but it gets the job done when needed.

It was recently announced that the Context is getting a new API, which implements the provider pattern out of the box.

If you are using things like React Redux or Apollo, you might be familiar with the pattern.

Seeing how it works with today’s API will help you understand the new API as well. You can play around with the following sandbox.

The top-level component — called Provider — sets some values on the context. The child components — called Consumers — will grab those values from the context.

The current context syntax is a bit strange, but the upcoming version is implementing this exact pattern.


High Order Components

Let’s talk about reusability. Together with dropping the old React.createClass() factory, the React team also dropped the support for mixins. They were, at some point, the standard way of composing components through plain object composition.

High Order Components — HOCs from now on — went out to fill the need for reusing behavior across multiple components.

A HOC is a function that takes an input component and returns an enhanced/modified version of that component. You will find HOCs under different names, but I like to think of them as decorators.

If you are using Redux, you will recognize that the connect function is a HOC — it takes your component and adds a bunch of props to it.

Let’s implement a basic HOC that can add props to existing components.

If you like functional programming, you will love working with high order components. Recompose is a great package that gives you all these nice utility HOCs like withProps, withContext, lifecycle, and so on.

Let’s have a look at a very useful example of reusing functionality.

You can use withAuthentication when you want to render sensitive content inside a route. That content will only be available to logged-in users.

This is a cross-cutting concern of your application implemented in a single place and reusable across the entire app.

However, there is a downside to HOCs. Each HOC will introduce an additional React Component in your DOM/vDOM structure. This can lead to potential performance problems as your application scales.

Some additional problems with HOCs are summarized in this great article by Michael Jackson. He advocates replacing HOCs with the pattern we’ll be talking about next.


Render Props

While it is true that render props and HOCs are interchangeable, I don’t favor one over another. Both patterns are used to improve reusability and code clarity.

The idea is that you yield the control of your render function to another component that then passes you back the control through a function prop.

Some people prefer to use a dynamic prop for this; some just use this.props.children.

I know, it’s still very confusing, but let’s see a simple example.

Here we are using children as the render prop. Inside the <ScrollPosition> component we will send a function which receives the position as a parameter.

Render props can be used in situations where you need some reusable logic inside the component and you don’t want to wrap your component in a HOC.

React-Motion is one of the libraries that offer some great examples of using render props.

Finally, let’s look at how we can integrate async flows with render props. Here’s a nice example of creating a reusableFetch component.

I’m sharing a Sandbox link so you can play with it and see the results.

You can have multiple render props for the same component. With this pattern, you have endless possibilities of composing and reusing functionality.

What patterns do you use? Which of them would fit in this article?

Better Programming

Advice for programmers.

Alex Moldovan

Written by

Full-steak 🥩 developer, #javascript, #react, #graphql, #webperf. Founder of @jsheroes. I never wear matching socks🧦

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade