9

Write better React, compose multiple functional HoCs, Higher-Order Components

 2 years ago
source link: https://itnext.io/write-better-react-compose-multiple-functional-hocs-higher-order-components-442a11bd2e86
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Write better React, compose multiple functional HoCs, Higher-Order Components

1*eKp6CeYY-krBEKhXLz6oFg.jpeg

In a previous article, I wrote about the concept of chaining functions using pipe and compose. Today I would like to extend this topic by providing you some scenarios where I found function composition can become really handy in the everyday life of a front-end developer using React applying multiple Higher-Order Components in a more functional way!

What is a Higher-Order Function (HOF)

Before diving deep into Higher-Order Components you should be familiar with the meaning of Higher Order Functions that we can describe as a function that does at least one of the followings:

  • Takes one or more functions as arguments
  • Returns a function as its result

Let’s take for example one of the standard ES Higher-Order Function you are probably already familiar with: Array.prototype.map, it takes a function as an argument which is used as a callback and applies it to every element of the array. A very quick reminder:

We can now write a custom Higher-Order Function:

Obviously, this is a dead-simple example but there are many applications for Higher-Order Functions, the benefit of this approach is that you can reuse the HoF providing different operation functions, reducing code duplication in favor of the single responsibility principle.

Higher-Order Components with React

Higher-Order Components are very similar to Higher-Order Functions, here is the definition from the React documentation: “Concretely, a higher-order component is a function that takes a component and returns a new component.”.

A simple example will be very useful here, let’s first define a standard component that we will wrap later into a hoc:

Imagine you want this component to be enhanced with some kind of information, in this very simple example we are passing a custom prop, a static user that in a real application you want to be fetched in some way:

Now we can wrap the App component with our newly created HoC:

Every component in your application wrapped by the “withUser” HoC, will have the currentUser prop. If we have a very complex logic this could be a very good pattern to avoid code duplication. You can have a look at a lot of real-life examples of this in the Klarna repository:

https://github.com/klarna/higher-Order-components

Composing multiple HoCs

What if we want a Component to be wrapped by multiple HoCs? Well, here we have compose at the rescue (they are explained in depth in my previous article). Let’s create another simple HoC:

now we can wrap our two HoC’s together (we can use the Ramda function compose instead of creating our custom one)

I created a Code Sandbox so you can play around with the code:

Recap

Higher-order components are really useful for abstracting logic, for example, most of your pages will have the same layout, and maybe they share the same elements; they are easy to handle, they make code more readable and they do not mutate the original component meaning they are pure functions.

Thanks for reading if you have come so far ❤

During this post, we have used very simple components to explain some complex concepts and share a pattern that you may find useful.
Some references to go deeper into the main topics of this article:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK