Delving Developer

How to Render an Array of Components in React

Eddie Cunningham
Eddie Cunningham
4 min readReact.js
Cover Image for How to Render an Array of Components in React

If you have ever had the need to render multiple components in React, you have probably come across the task of rendering an array of components. While it may seem like a simple task, there are some caveats and edge cases to consider. In this article, we will go over the best practices and edge cases when rendering an array of components in React.

Creating an Array of Componentslink

First, let's create an array of components. Here's an example array that contains three components:

const components = [
  <MyComponent key={0} />,
  <MyComponent key={1} />,
  <MyComponent key={2} />,
];

Note that each component has a key prop. This is important because without it, React will give a warning. The key prop tells React to keep track of each component in the array and re-render only the ones that have changed.

Rendering an Array of Componentslink

Now that we have our array of components, we can render them in our JSX. There are a few ways to do this, but the most common is using the map() method:

function App() {
  const components = [
    <MyComponent key={0} />,
    <MyComponent key={1} />,
    <MyComponent key={2} />,
  ];

  return <div>{components.map((component) => component)}</div>;
}

This will render all three components as expected. However, there are a few caveats to consider.

Caveats and Edge Caseslink

The Order of Keys Must Not Change

One of the most common mistakes when rendering an array of components is changing the order of the key props. The order of the key props must remain the same between renders, or else React will think that each component is new and re-render them all.

Children Must Be Unique

Another mistake is passing the same component twice.

const components = [
  <MyComponent key={0} />,
  <MyComponent key={1} />,
  <MyComponent key={2} />,
  <MyComponent key={2} />, // Duplicate element, will cause issues
];

React will not warn you about this, but it could cause issues in your application.

Array length should be limited

While it is possible to render very, very large arrays of components, it is not advisable, and in some edge cases, it can cause your application to run slow. Instead of rendering all the components at once, consider rendering a few and then rendering more as the user scrolls.

Handling Props

One of the most important considerations when rendering an array of components is how to handle props. In most cases, you will want to pass different props to each component in the array. Here's an example of how to do this:

function App() {
  const components = [
    { label: "Item 1" },
    { label: "Item 2" },
    { label: "Item 3" },
  ];

  return (
    <div>
      {components.map((component, index) => (
        <MyComponent key={index} label={component.label} />
      ))}
    </div>
  );
}

This will pass the label prop to each component based on the current index of the array.

Wrapping Components

If you need to wrap each component with additional markup, you can use a higher-order component (HOC). Here's an example:

function withWrapper(WrappedComponent) {
  return function WrappedWithWrapper(props) {
    return (
      <div className="wrapper">
        <WrappedComponent {...props} />
      </div>
    );
  };
}

function App() {
  const components = [
    { label: "Item 1" },
    { label: "Item 2" },
    { label: "Item 3" },
  ];

  const WrappedComponent = withWrapper(MyComponent);

  return (
    <div>
      {components.map((component, index) => (
        <WrappedComponent key={index} label={component.label} />
      ))}
    </div>
  );
}

Here, we define a HOC called withWrapper that wraps its children with a <div> element with a class of wrapper. Then, we create a new component called WrappedComponent that is the result of invoking withWrapper with MyComponent as its argument. Finally, we render WrappedComponent for each element in the array, passing the label prop as we did before.

Conclusionlink

Rendering an array of components is a common task in React, but it's important to consider the caveats and edge cases to ensure that your application remains performant and bug-free. Remember to always include a key prop, ensure that the key order does not change, and ensure that no duplicates are passed. Additionally, consider the number of components you are rendering and whether it is advisable to limit the array's length, and make sure to handle props and wrapping correctly. By following these best practices, you can render arrays of components in a safe and performant way.