Think of it like you're just calling JavaScript functions. You can't use a for loop where the arguments to a function call would go:

return tbody(
    for (let i = 0; i < numrows; i++) {
        ObjectRow()
    } 
)

See how the function tbody is being passed a for loop as an argument – leading to a syntax error.

But you can make an array, and then pass that in as an argument:

const rows = [];
for (let i = 0; i < numrows; i++) {
    rows.push(ObjectRow());
}
return tbody(rows);

You can basically use the same structure when working with JSX:

const rows = [];
for (let i = 0; i < numrows; i++) {
    // note: we are adding a key prop here to allow react to uniquely identify each
    // element in this array. see: https://reactjs.org/docs/lists-and-keys.html
    rows.push(<ObjectRow key={i} />);
}
return <tbody>{rows}</tbody>;

Incidentally, my JavaScript example is almost exactly what that example of JSX transforms into. Play around with Babel REPL to get a feel for how JSX works.

Answer from Sophie Alpert on Stack Overflow
🌐
Upmostly
upmostly.com › home › tutorials › how to for loop in react (with examples)
How to Use For Loop in React (with Code Examples)
October 28, 2021 - Let’s begin by exploring how we can use the Map iterator to loop through elements in an array and render out some HTML for each of those elements. Introduced in ES6, the Map array function is by far my most used method of iterating over an ...
Discussions

reactjs - Is it a bad idea to use for loop/non ES6 for React - Stack Overflow
I have two props that both are arrays. prop1 = json objects array prop2 = components array (as they are icons in svg format). What I want to do is iterate prop1 and insert icons in props2 as I go... More on stackoverflow.com
🌐 stackoverflow.com
For loops ever in React?
Hey guys, weird function I got going on here that I put in it’s own component. If I pass in a number, it creates that many spans. Is there any way not to use a for loop here or is this the only time when you have nothing… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
February 3, 2020
reactjs - javascript (ES6): any more efficient way than "for loops"? - Stack Overflow
this is not a duplicate. please see my comment below! does somebody knows an more efficient solution than for loops in ES6? I have written the following, which lacks of performance. Any improvement More on stackoverflow.com
🌐 stackoverflow.com
Modify React Elements in ES6 using a for loop and setTimeout
I am trying to create a typewriter animation like this in my es6 component (essentially, iteratively renders additional passed elements or letters). However, any time I execute / render this compo... More on stackoverflow.com
🌐 stackoverflow.com
October 7, 2017
🌐
Ronald James
ronaldjamesgroup.com › blog › looping-inside-react-jsx
Looping inside React JSX | Ronald James
var rows = []; for (var i = 0; i < numrows; i++) { // note: we add a key prop here to allow react to uniquely identify each // element in this array. see: https://reactjs.org/docs/lists-and- keys.html rows.push(); } return {rows}; Incidentally, ...
🌐
Sentry
sentry.io › sentry answers › react › how do you loop inside react jsx?
How do you loop inside React JSX? | Sentry
If you have a set of elements, you can use normal JavaScript for, for of, and for in loops outside the return block. Inside the return block, you can use the resulting array. You can loop over an array and create JSX partials like so: import ...
🌐
Stack Overflow
stackoverflow.com › questions › 56198592 › is-it-a-bad-idea-to-use-for-loop-non-es6-for-react
reactjs - Is it a bad idea to use for loop/non ES6 for React - Stack Overflow
13 for loop in react · 0 es6 for loop not looping · 4 javascript (ES6): any more efficient way than "for loops"? 0 Code optimization for React (forEach or simply for) 2 Is it okay to use map as a for loop without returning · 2 `for` loops vs `.map` to iterate arrays ·
🌐
freeCodeCamp
forum.freecodecamp.org › t › for-loops-ever-in-react › 346935
For loops ever in React? - The freeCodeCamp Forum
February 3, 2020 - { let spans = [] for (let i = 0; i ) } return spans } export default SpanMaker I think another thing to note here is that I am not updating...
🌐
iFour Technolab
ifourtechnolab.com › blog › loops-in-react-jsx-a-deep-dive-into-the-basics
Loops in React JSX: A deep dive into the basics | iFour Technolab
October 20, 2022 - You may have observed that we did not generate li elements 5 times. We iterated the array with a map, and the map returned the new element. The output will be as follows: Figure 2. using the map Method ... Users can use the standard “for” loop for creating the li element.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › loop-inside-react-jsx
Loop Inside React JSX | GeeksforGeeks
May 8, 2024 - The approaches to loop inside the React JSX are given below: ... In this approach, we are using Array.map() inside JSX to dynamically render a list of numbers from the state array numbers. Each number is displayed as an <li> element with inline styling for color, margin, and cursor.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › alternatives-of-for-loops-and-if-else-blocks-in-reactjs
Alternatives of for loops and if-else blocks in ReactJS - GeeksforGeeks
July 23, 2025 - By using Map you can do almost anything that a for loop does. Also, Map returns an array so there won't be any Compilation Error. Don't forget to add a key prop while returning every component. ... // Filename - App.js import React from "react"; function App() { const items = [1, 2, 3, 4, 5]; return ( <div> {items.map((item, index) => ( <div key={index}> Hello World {item} </div> ))} </div> ); } export default App;
🌐
CoreUI
coreui.io › blog › how-to-loop-inside-react-jsx
How to loop inside React JSX · CoreUI
September 27, 2024 - This guide will cover the different ways to render lists and elements using loops in React, focusing on the use of the map function, traditional loops, and best practices. Let’s explore how to create components dynamically and render lists efficiently using React JSX. ... JSX (JavaScript XML) is a JavaScript syntax extension that allows you to write HTML-like code within JavaScript files. However, normal JavaScript constructs, like for loops, aren’t directly supported within JSX because they are statements, not expressions.
🌐
Dayhaysoos
dayhaysoos.com › blog › rendering-components-in-react-with-loop
Rendering components in React with a loop
Doing this would take you all day, it would also be hard to make edits if a bunch of the values changed for some reason. This is where you’d want to use a loop. ... I call this opening up the JavaScript portal. This allows you to use JavaScript within JSX. I used map on arrayOfStrings, the paramater string represents the element in the array at the current iteration of the loop, which is a string, and the ES6 arrow is shorthand for returning that string, but wrapped in the <p /> tag.
Top answer
1 of 6
287

Updated: As of React > 0.16

Render method does not necessarily have to return a single element. An array can also be returned.

var indents = [];
for (var i = 0; i < this.props.level; i++) {
  indents.push(<span className='indent' key={i}></span>);
}
return indents;

OR

return this.props.level.map((item, index) => (
    <span className="indent" key={index}>
        {index}
    </span>
));

Docs here explaining about JSX children


OLD:

You can use one loop instead

var indents = [];
for (var i = 0; i < this.props.level; i++) {
  indents.push(<span className='indent' key={i}></span>);
}
return (
   <div>
    {indents}
    "Some text value"
   </div>
);

You can also use .map and fancy es6

return (
   <div>
    {this.props.level.map((item, index) => (
       <span className='indent' key={index} />
    ))}
    "Some text value"
   </div>
);

Also, you have to wrap the return value in a container. I used div in the above example

As the docs say here

Currently, in a component's render, you can only return one node; if you have, say, a list of divs to return, you must wrap your components within a div, span or any other component.

2 of 6
51

Here is more functional example with some ES6 features:

'use strict';

const React = require('react');

function renderArticles(articles) {
    if (articles.length > 0) {      
        return articles.map((article, index) => (
            <Article key={index} article={article} />
        ));
    }
    else return [];
}

const Article = ({article}) => {
    return ( 
        <article key={article.id}>
            <a href={article.link}>{article.title}</a>
            <p>{article.description}</p>
        </article>
    );
};

const Articles = React.createClass({
    render() {
        const articles = renderArticles(this.props.articles);

        return (
            <section>
                { articles }
            </section>
        );
    }
});

module.exports = Articles;
🌐
Medium
medium.com › @rana.adnanali › how-to-write-for-loop-in-react-js-83ef4f67d9f7
How to Write for Loop in React JS | by Rana Adnan | Medium
August 8, 2023 - ES6 brought several enhancements to JavaScript, including more concise ways to write loops. Utilizing these features can lead to cleaner and more readable code. While React provides tools for iteration, there are also external libraries available ...
🌐
Reddit
reddit.com › r/react › creating objects with for loop
r/react on Reddit: Creating objects with for loop
March 31, 2022 -

i am trying to create a site with a database where you are able to see the data from the database. When i get the data i do a for loop for the data and create the elements but i dont know how i can show them withn a for loop because i dont know a good way to do this in the render function.The Code:

function ReadData(data {)
for (let i = 0; i < data.length; i++ { 
var Date = data[i].created; 
const Button = React.createElement('div', { id: "But " + data[i].id, className: "PostButton", onClick: (event) => event.stopPropagation((window.location.href = Post?/${data[i].name}/${data[i].id}))});
const Main = React.createElement('div', { id: "post " + data[i].id }); //document.getElementById(But ${data[i].id}).appendChild(Main); 
const info = React.createElement('div', { id: "Post Info " + i }); //document.getElementById("post " + data[i].id).appendChild(info); 
const Profile = React.createElement('button', { id: "NameDisplay", className: "SameLine", onClick: (event) => event.stopPropagation(window.location.href = Profile?${data[i].name})}, ${data[i].name});   } })

some of the code like the onclick does not work but that is because this was first a normal javascript project but i am now moving it to react so you see a window.location.href while this does not work with react but ignore that part.

i had working js code for the regular site but that create multiple objects for some reason that code looked like this:

function ReadData(data){   
for (let i = 0; i < data.length; i++) {       
var Date = data[i].created;        
let Button = document.createElement("div");       
Button.id = "But " + data[i].id;       
Button.className = "PostButton";       
Button.onclick = function () { event.stopPropagation(window.location.href = `Post?/${data[i].name}/${data[i].id}`) }       document.getElementById("Posts").appendChild(Button);        
let Main = document.createElement("div");       
Main.id = "post " + data[i].id;       
document.getElementById(`But ${data[i].id}`).appendChild(Main);        
let info = document.createElement("div");       
info.id = "Post Info " + i;       
document.getElementById("post " + data[i].id).appendChild(info);        
info.innerHTML = `<button class='SameLine' id='NameDisplay' onclick="event.stopPropagation(window.location.href='Profile?${data[i].name}')">${data[i].name}</button>`   } }

🌐
Medium
medium.com › frontendweb › how-to-use-loops-in-react-js-4953cc2ff0c7
How to use loops in React.js?
December 25, 2022 - It take time create or recreate app with for and while loop. You can use forEach() directly in reactjs.
🌐
Reddit
reddit.com › r/reactjs › can i use a for loop with jsx? to render something 3 times?
r/reactjs on Reddit: Can I use a for loop with JSX? To render something 3 times?
August 31, 2021 -
let sampleOutput =()=>{
  return
    for(let i=0;i<3;i++){<p>hi</p>}  
}


trying to display hi 3 times inside

return()

in my component. I know how to .map or .forEach for objects and arrays but what if my store or variable is just a number, I assume forloop doesnt work?

🌐
Intellipaat
intellipaat.com › home › blog › how to use for loop in react? (with examples)
How to Use For Loop in React? (With Examples) | Intellipaat
April 15, 2025 - You can use a for loop in React using the map() method on the array. The for loop allows you to repeat a code block for a specific number of times.