It looks like you are trying to use a React component as if it were a React hook.

My suggestion would be to re-write as a React hook and then you should be able to correctly log the chosen pokemon:

Landing Becomes:

import { usePokemon } from "../../component/returnPokemon";

export const Landing = () => {
  const { chosenPokemon } = usePokemon("ditto");
  console.log(chosenPokemon);
  return (
    <>
      <h1>Landing</h1>
      {chosenPokemon.name}
    </>
  );
};

Instead write ReturnPokemon as a React Hook:

import { useDitto } from "../../context/ditto";
import { useCharizard } from "../../context/charizard";

export function usePokemon(pokemon) {
  const { ditto } = useDitto();
  const { charizard } = useCharizard();

  if (pokemon === "ditto") {
    return { chosenPokemon: ditto };
  } else if (pokemon === "charizard") {
    return { chosenPokemon: charizard };
  } else {
    return null;
  }
};
Answer from Jack Gore on Stack Overflow
🌐
egghead.io
egghead.io › lessons › react-return-a-list-of-elements-from-a-functional-component-in-react
Return a list of elements from a functional component in React | egghead.io
[00:39] To solve this problem, instead of returning this div, we throw an array of those div elements, those boxes. Of course, add commas. After we save it, refresh, we are going to see those elements displayed just fine.
Published   December 9, 2018
Top answer
1 of 2
1

It looks like you are trying to use a React component as if it were a React hook.

My suggestion would be to re-write as a React hook and then you should be able to correctly log the chosen pokemon:

Landing Becomes:

import { usePokemon } from "../../component/returnPokemon";

export const Landing = () => {
  const { chosenPokemon } = usePokemon("ditto");
  console.log(chosenPokemon);
  return (
    <>
      <h1>Landing</h1>
      {chosenPokemon.name}
    </>
  );
};

Instead write ReturnPokemon as a React Hook:

import { useDitto } from "../../context/ditto";
import { useCharizard } from "../../context/charizard";

export function usePokemon(pokemon) {
  const { ditto } = useDitto();
  const { charizard } = useCharizard();

  if (pokemon === "ditto") {
    return { chosenPokemon: ditto };
  } else if (pokemon === "charizard") {
    return { chosenPokemon: charizard };
  } else {
    return null;
  }
};
2 of 2
0

There is no need to use useState if you are going to return it anyway. You cannot return an object in a component. You have to return some JSX element like and you can have your object inside.

Assumptions made here:

  • ditto, charizard are strings. if not just do JSON.stringify(ditto) to cast object as string

Landing:

export const Landing = () => {
  return (
    <>
      <h1>Landing</h1>
      <ReturnPokemon pokemon="ditto" />
    </>
  );
};

ReturnPokemon:

import { useDitto } from "../../context/ditto";
import { useCharizard } from "../../context/charizard";

export const ReturnPokemon = ({ pokemon }) => {
  const { ditto } = useDitto();
  const { charizard } = useCharizard();

  if (pokemon === "ditto") {
    return <div>{ditto}</div>;
  }

  if (pokemon === "charizard") {
    return <div>{charizard}</div>;
  }

  return <div></div>;
};
Discussions

reactjs - Why can't a react stateless function return an array? - Stack Overflow
Should anyone care for more detail, ... are just components with only a render method. – Josh David Miller Commented Jan 15, 2016 at 17:45 · Thanks for the links @JoshDavidMiller! ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 583 React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing · 790 React Hooks: useEffect() is called twice even if an empty array is used as ... More on stackoverflow.com
🌐 stackoverflow.com
August 1, 2017
[@types/react] React Component does not allow returning an array of React Elements
If you know how to fix the issue, make a pull request instead. I tried using the @types/react (version 16.9.19) package and had problems. I tried using the latest stable version of tsc (version 3.7... More on github.com
🌐 github.com
3
September 24, 2019
reactjs - Typescript + React, rendering an array from a stateless functional component - Stack Overflow
You can now return an array of elements from a component’s render method. This works in typescript for regular class components, but I cannot get it to work for stateless functional components. See this repo if you want to reproduce for yourself. ... import * as React from 'react'; // See ... More on stackoverflow.com
🌐 stackoverflow.com
React functional components: return an array list inside an array of nested objects
I have an array of objects that have themselves an array inside and I would like to create a list with first some top-level object property and then map over the array elements of each object and More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/react › a react component can also return an array of elements:
r/react on Reddit: A React component can also return an array of elements:
April 25, 2022 -

if found this in react docs here is the link does this works i tried but it is not working

link to doc

render() { // No need to wrap list items in an extra element! return [ // Don't forget the keys :) <li key="A">First item</li>, <li key="B">Second item</li>, <li key="C">Third item</li>, ]; }
🌐
GitHub
github.com › DefinitelyTyped › DefinitelyTyped › issues › 41808
[@types/react] React Component does not allow returning an array of React Elements · Issue #41808 · DefinitelyTyped/DefinitelyTyped
September 24, 2019 - const ExampleComponent: FC<{}> = () => { // some data example const data = [{ name: '123' }, { name: '321' }]; // return React Elements array return data.map(t => <div key={t.name}>{t.name}</div>); }; ... TS2322: Type '() => JSX.Element[]' is not assignable to type 'FC<{}>'. Type 'Element[]' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>'.
Author   Arttse
🌐
DEV Community
dev.to › alexmercedcoder › ultimate-2021-reference-for-react-functional-components-1c33
Ultimate 2021 Reference for React Functional Components - DEV Community
November 4, 2020 - You can pass arrays of JSX if you want. return [<h1>Hello World</h1>, <h1>Hello World</h1>, <h1>Hello World</h1>] ... Your JSX is treated as html, and anything in curly brackets are treated as Javascript expressions in the functions scope. Any valid javascript expression can be used this way. ... Props allows a component to receive data from its parent component.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 56767369 › react-functional-components-return-an-array-list-inside-an-array-of-nested-obje › 56767501
React functional components: return an array list inside an array of nested objects
import React from 'react'; import Anzeige from '../pages/gebuchte-stellenanzeigen'; const anzeigenArray = (props) => { let style; if (props.aussehen != null) { style = props.aussehen; }else { style = {}; } let docs = props.anzeigenArray; const filterByDay = (entr, idx) => { const dayInMilliseconds = 24*60*60*1000; let now = new Date().getTime(); let previous = idx; let next = idx+1; let datenow = now - (previous*dayInMilliseconds); let datethen = now - (next*dayInMilliseconds); let arr = entr.filter((anzeige) => { let anzeigeDate = new Date(anzeige.createdtimestamp).getTime(); return ( anzeige
🌐
Pluralsight
pluralsight.com › blog › guides
Manipulating Arrays and Objects in State with React | Online Courses, Learning Paths, and Certifications - Pluralsight
July 27, 2020 - The snippet below is the class-based ... component. The useState hook is a function that takes in a default value as a parameter (which can be empty) and returns an array containing the state and a function to update it....
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>
   );
}
🌐
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 - You can dynamically create the array of components as shown below: import React from "react" const Item = ({ value }) => { return <li>{value}</li> } const App = () => { const items = Array.from({ length: 10 }).map((_, index) => ( <Item key={index} value={index + 1} /> )) return <ul>{items}</ul> } export default App ·
🌐
pawelgrzybek
pawelgrzybek.com › return-multiple-elements-from-a-component-with-react-16
Return multiple elements from a component with React 16 | pawelgrzybek.com
const App = () => [ <p key="1">React ... and manually added keys to each of the element, you can use an Aux helper function that simply returns all its children....
🌐
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]
June 26, 2019 - Step 5: Return arrayDataItems from the component wrapped in <ul> To iterate through an array of objects in ReactJS, you must use the mao() method. It creates a new array by applying a provided function to each element of the original array.
🌐
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
November 5, 2021 - You can use this data to return a new value from your function which Array.prototype.map will then use to add into a new array in the same position/index.
🌐
DigitalOcean
digitalocean.com › community › conceptual-articles › understanding-how-to-render-arrays-in-react
Understanding How To Render Arrays in React | DigitalOcean
June 15, 2022 - You may notice writing React.fragment is more tedious than adding a <div>. Fortunately, the React team developed a shorter syntax to represent this component. You can use <> </> in place of <React.Fragment></React.Fragment>: function ReptileListItems() { return ( <> <li>alligator</li> <li>snake</li> <li>lizard</li> </> ); } In this article, you explored various examples of how to render arrays in a React application.
🌐
Medium
medium.com › @arunpaneru01 › returning-react-components-beyond-the-parent-container-15794fbda014
Returning array of components in React | Arun Paneru | Nov 2024 | Medium
February 27, 2024 - By using fragments, React renders ... as use of root element is not the only working option. Returning an array of sibling components is also a valid solution ....
🌐
Stack Overflow
stackoverflow.com › questions › 53925423 › how-to-map-function-returning-array-in-react
How to map function returning array in react
November 1, 2022 - Remove this. if data is just a function in scope without any context as exemplified in your question. ... It seems like this is what you are looking for. class App extends React.Component { render() { return( <div> {data().map(item => { return ( <div>{item.id} {item.name} </div> ) })} </div> ) } } function data() { return ( [ {id: 1, name: "Jack"}, {id: 2, name: "Mark"}, {id: 3, name: "Mike"}, {id: 4, name: "Russell"} ] ) }
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

🌐
DEV Community
dev.to › ajithmadhan11 › rendering-arrays-in-react-14bh
Rendering Arrays in React - DEV Community
October 3, 2025 - To return multiple element in react we can loop over the array using the map() method and return single element. export default function App() { const animalList=['Lion','Tiger','Elephant','Giraffe']; return ( <div className="App"> <h1>Render ...