Make your React components more flexible using polymorphism
Tags: react, fragment, grid, layout
Published: 2022-10-24 15:03:00

---
title: Make your React components more flexible using polymorphism
published: true
description: Apply layout CSS properties to components that have multiple elements
tags: react, fragment, grid, layout
cover_image: https://images.unsplash.com/photo-1612855190135-bdbacda2a2b5?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80
# published_at: 2022-10-21 21:27 +0000
---


When we're working with React, it's natural to break the UI into a component hierarchy. So you need to reuse an element across your app, you make it a component.

As requirements evolve and new UIs are created, you may need a piece of UI that is almost the same that you created before, but in a grid layout. If you decide to handle that inside the component, things can become quite tricky. Let's look into an approach to solve that problem by using [Polymorphic Components](https://www.freecodecamp.org/news/build-strongly-typed-polymorphic-components-with-react-and-typescript/#what-are-polymorphic-components) (a.k.a. the "as" prop).

Take for example an "Input" component that accepts a `label` prop and renders that label above the text input.

```js
// the input code
export const Input = ({ id, label }) => {
  return (
    <div>
      {label && <label htmlFor={id}>{label}</label>}

      <div>
        <input id={id} type="text" />
      </div>
    </div>
  );
};

// using it in some form
export const Form = ({ id, label }) => {
  return (
    <div>
      <Input id="input-1" label="My Label" />
    </div>
  );
};
```

![Single Input](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n77px2ss3ycgbbowmqrk.png)

Cool, it's done. Now you can add your custom styles and reuse it everywhere!

A few weeks later, you're building a new form, and now there is a grid of labels and inputs, the labels need to be horizontally aligned with the text inputs and each text input needs to be aligned vertically with the other inputs:

![Multiple inputs aligned](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpgpew0hbopu9fxev7gs.png)

How do you handle that? Add a boolean condition? Create a new component? But how do you make sure that the labels will align? **Polymorphism to the rescue!**

Let's enable the `as` prop for our component and use it with a [Fragment](https://beta.reactjs.org/apis/react/Fragment#fragment):

```javascript
// enable an `as` prop that defaults to "div", the element used before
// if an element is passed, that element will be used instead
export const Input = ({ id, label, as = "div" }) => {
  const Tag = as; // use that as the tag for your wrapper element

  return (
    <Tag>
      {label && <label htmlFor={id}>{label}</label>}

      <div>
        <input id={id} type="text" />
      </div>
    </Tag>
  );
};

// now, in your form, set the "as" prop as a React Fragment
export const Form = ({ id, label }) => {
  return (
    {/* The `Grid` class has our CSS Grid Layout */}
    <div className="Grid">
      <Input id="input-1" label="My Label" as={Fragment} />
      <Input id="input-2" label="Other Label" as={Fragment} />
    </div>
  );
};
```

As the fragment has no effect on the resulting DOM, we are able to apply our grid layout as if all the labels and the inputs were used directly in the div that has the "Grid" class. That avoids adding more conditions to your components and making sure that all Inputs align correctly.

### Thanks for reading!

How would you solve that layout problem? Have you used polymorphism before?

Let's keep in touch! Here's my [Twitter](https://twitter.com/julioxavierr).