Certainly.

If you want to pass instances of the component, what you're doing is fine except that you need to close those JSX tags:

{
  src: <SVGComponent1 />,
  ...

Or if you want to pass the component class/function itself:

{
  src: SVGComponent1,
  ...

It's not totally clear what you're trying to do in your usage, though -- what are you hoping to achieve by passing a component to the src attribute of an img tag?

Perhaps that's old code you forgot to update for passed components and you mean this:

  const slides = items.map((item, index) => {
      return (
        <CarouselItem
          onExiting={this.onExiting}
          onExited={this.onExited}
          key={index}
        >
          {item.src}
          <CarouselCaption captionText={item.caption} captionHeader={item.caption} />
        </CarouselItem>
      );
    });

This ought to work just fine when a component instance was passed.

Note I also changed the key attribute, since I'm not sure passing a component to that will work!

Answer from tremby on Stack Overflow
🌐
Reddit
reddit.com › r/reactjs › how do i save an array of react components into localstorage?
r/reactjs on Reddit: How do I save an array of React Components into localStorage?
January 10, 2021 -

I have an array of React Components that looks like this:

const [allSongs, setAllSongs] = useState([<Song/>,<Song/>])

but when I store it to localStorage with JSON.stringify and try to retrieve the list with JSON.parse, the array children have changed slightly, meaning I can't map over the elements to display them anymore:

localStorage.setItem('allSongs', JSON.stringify(allSongs))
const deSerializedArr = JSON.parse(localStorage.getItem('allSongs'))

console.log(allSongs)
//output is list of these --> {$$typeof: Symbol(react.element), type: function, key: null, ref: null, props: {}, …}

console.log(deSerializedArr)
// output is list of these --> {key: null, ref: null, props: {}, _owner: null, _store: {}}

Side note: It might be a bad practice to have an array of react components which I will later map over to display, but undoing this would require me to rewrite a lot other code

Discussions

How to store components in an array and render them when I need in React?
I want to render x numbers times a specific component according to what the user chooses. I wrote this function: const generateShiftsForm = (totalWeeks) => { const shiftWeeks = 1; More on stackoverflow.com
🌐 stackoverflow.com
How to Load a react components in an array and display them
So I have a list of components that I want to load and store in an array e.g array = [ , , ,] so this is my code and how I have structed every... More on stackoverflow.com
🌐 stackoverflow.com
In ReactJS, can I declare an array of components? and return certain component - Stack Overflow
can I declare an array of components? and return certain component... is this possible?? ES6: import React from 'react'; import Component1 from './Component1'; import Component2 from './Compo... More on stackoverflow.com
🌐 stackoverflow.com
Rendering React Components from Array of Objects
How can I loop through this data ... of the array? ... I could be wrong but I think you need to use stationsArr instead of stations inside the render function. ... You can map the list of stations to ReactElements. With React >= 16, it is possible to return multiple elements from the same component without needing ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 10
21

Have you consider using the new React Fragments? (in v16)

This would be the simplest solution as it would by pass the whole array/key issue.

If you need to pass key, then I'd suggest to simply require the components to have the keys. This is how React works, so I wouldn't suggest you to hide this behavior behind an interface that might not be predictable.

If you really need to do this, then you can use React.cloneElement to clone the element and inject new properties:

React.cloneElement(element, { key: 'foo' });
2 of 10
15

If you’re always going to want to render all the components in your components file then you’re probably better off wrapping them in a React.Fragments tag.

Best practise is just to export this as a simple function that returns the components rather than as a constant.

So...

const Components = props => {
  return (
    <React.Fragment>

      <ComponentOne/>
      <ComponentTwo/>

    </React.Fragment>
  )
}

export default Components

That allows you to put multiple components next to each other without a DOM element containing them.

You should then just be able to render that by using it as a normal component and it’ll render all of them, so just import it then...

<Components />

Otherwise, if you want to treat them like an array, you have a function for free on the React object you’ve imported...

React.Children.toArray(arrayOfComponents)

You pass it an array of components (like in your original question) and it allows you to sort and slice it if you need to then you should be able to just drop it in the return of your render function

🌐
Atomizedobjects
atomizedobjects.com › blog › react › how-to-render-an-array-of-components-in-react
How to render an array of components in React | Atomized Objects
To render an array of components in React you simply need to pass the array into JSX by wrapping it in curly braces, just be sure that your components each have a unique key prop because React will use this when rendering it to avoid bugs.
🌐
React
react.dev › learn › rendering-lists
Rendering Lists – React
How to create arrays of filtered items with JavaScript’s filter(). Why and how to set key on each component in a collection so React can keep track of each of them even if their position or data changes.
🌐
DEV Community
dev.to › collegewap › how-to-render-array-of-components-in-react-fma
How to render array of components in React - DEV Community
April 9, 2023 - ... import React from "react" const ... <ul>{items}</ul> } export default App · All you need to do is add the array within the flower brackets {items}....
🌐
Pluralsight
pluralsight.com › blog › guides
Add Data into an Array in a State Object | Online Courses, Learning Paths, and Certifications - Pluralsight
November 18, 2020 - The content of an array can be traversed and transformed into UI elements using the map function, which will apply the passed transformer function to each array element and return a list of elements.
Find elsewhere
🌐
Pluralsight
pluralsight.com › blog › guides
Manipulating Arrays and Objects in State with React | Online Courses, Learning Paths, and Certifications - Pluralsight
November 4, 2020 - Using hooks, you can easily apply that to your state array, as shown below. The values can now be used to render a list of paragraph tags using the map function. import React, { useState, useEffect } from 'react'; const ProductsPage = () => { const [productsList, setProductsList] = useState([]); const [isLoading, setisLoading] = useState(true); useEffect(() => { fetch('http://127.0.0.1:8000/api/v1/products/all') .then((res) => res.json()) .then((data) => setProductsList([...data])) .then(setisLoading(false)); }, []); return ( <> <Header /> {isLoading ?
🌐
DigitalOcean
digitalocean.com › community › conceptual-articles › understanding-how-to-render-arrays-in-react
Understanding How To Render Arrays in React | DigitalOcean
July 27, 2020 - To render multiple JSX elements in React, you can loop through an array with the .map() method and return a single element. Below, you loop through the reptiles array and return a li element for each item in the array.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-render-array-of-components-in-react
How to Render an Array of Components in React
February 27, 2024 - In this tutorial, I will show you how using indices to render an array of components in React can be problematic. I'll also teach you how to mitigate array rendering issues with unique ids. As always, we'll work on a real world example and solve prob...
🌐
Medium
medium.com › react-in-the-real-world › stashing-jsx-in-arrays-the-magic-trick-of-react-components-42a62179b2d0
Stashing JSX in Arrays: The Magic Trick of React Components 🎩✨
July 30, 2023 - Stashing JSX in Arrays: The Magic Trick of React Components 🎩✨ Understanding the secret behind storing and rendering React function components in arrays. JSX element arrays, here we go! Hello …
🌐
Stack Overflow
stackoverflow.com › questions › 72600748 › how-to-load-a-react-components-in-an-array-and-display-them
How to Load a react components in an array and display them
useEffect(() => { for(let i=0;i<length;i++){ components[i] = <RadioButtonRN data={roadsigns.signs_questions[i].answers} box={false} textColor={primary} initial={2} selectedBtn={(e) => selectedAnswer(e)} /> } connsole.log(components) },[])
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-render-an-array-of-objects-in-reactjs
How To Render An Array Of Objects In ReactJS? | GeeksforGeeks
April 12, 2025 - The data is stored in an array of objects and displayed dynamically using the map() method. The RenderingArrayOfObjects component creates the table rows, and the App component displays the table with headers.
🌐
egghead.io
egghead.io › lessons › react-use-map-to-create-react-components-from-arrays-of-data
Use map to Create React Components from Arrays of Data | egghead.io
[00:54] Now that we've got that here in a render method, we can simply interpolate items.map. We can just map over those guys -- it's an array -- which will give us our item.
Published   March 28, 2014
🌐
Robin Wieruch
robinwieruch.de › react-state-array-add-update-remove
How to manage React State with Arrays - Robin Wieruch
May 17, 2020 - Then you use all remaining items to store them in React’s state. The first item isn’t used anymore. All previous array examples worked on an array of integers. Let’s see in a more complex scenario how to remove an object from a React state array instead. That example is better suited for a robust application, because we can work on identifiers instead of indexes. ... import React, { Component } from 'react'; class App extends Component { constructor(props) { super(props); this.state = { list: [ { id: '1', age: 42 }, { id: '2', age: 33 }, { id: '3', age: 68 }, ], }; } onRemoveItem = id => { this.setState(state => { const list = state.list.filter(item => item.id !== id); return { list, }; }); }; render() { return ( <div> <ul> {this.state.list.map(item => ( <li key={item.id}> The person is {item.age} years old.
🌐
CodingDeft
codingdeft.com › posts › react-render-array-components
How to render array of components in React | CodingDeft.com
April 7, 2023 - In this article, we will see how to do the same. ... This is required since we will be displaying a list of icons. ... All you need to do is add the array within the flower brackets {items}.
Top answer
1 of 6
197

You can map the list of stations to ReactElements.

With React >= 16, it is possible to return multiple elements from the same component without needing an extra html element wrapper. Since 16.2, there is a new syntax <> to create fragments. If this does not work or is not supported by your IDE, you can use <React.Fragment> instead. Between 16.0 and 16.2, you can use a very simple polyfill for fragments.

Try the following

// Modern syntax >= React 16.2.0
const Test = ({stations}) => (
  <>
    {stations.map(station => (
      <div key={station.call} className='station'>{station.call}</div>
    ))}
  </>
); 

// Modern syntax < React 16.2.0
// You need to wrap in an extra element like div here

const Test = ({stations}) => (
  <div>
    {stations.map(station => (
      <div className="station" key={station.call}>{station.call}</div>
    ))}
  </div>
); 

// old syntax
var Test = React.createClass({
    render: function() {
        var stationComponents = this.props.stations.map(function(station) {
            return <div className="station" key={station.call}>{station.call}</div>;
        });
        return <div>{stationComponents}</div>;
    }
});
 
var stations = [
  {call:'station one',frequency:'000'},
  {call:'station two',frequency:'001'}
]; 

ReactDOM.render(
  <div>
    <Test stations={stations} />
  </div>,
  document.getElementById('container')
);

Don't forget the key attribute!

https://jsfiddle.net/69z2wepo/14377/

2 of 6
61

I have an answer that might be a bit less confusing for newbies like myself. You can just use map within the components render method.

render () {
   return (
       <div>
           {stations.map(station => <div key={station}> {station} </div>)} 
       </div>
   );
}
🌐
GUVI
guvi.in › blog › programming languages › how to render an array of objects in react? [in 3 easy steps]
How to Render an Array of Objects in React? [in 3 easy steps]
October 21, 2025 - The most popular way to create a list or render an array of objects in React is using the array.map() method which takes an array of data and maps it to an array of components, so that each data item is represented by a corresponding component.