Languages
[Edit]
EN

React - loop inside JSX

5 points
Created by:
Dragontry
731

In this article, we would like to show you how to create a loop in React JSX.

Below example shows how to render specified number of Row components in our App in three steps:

  1. With Array(size) we create an empty array with the specified size,
  2. using fill() method we fill the array with undefined values,
  3. with map() we loop over the array rendering Row elements and assigning unique keys to them.

Practical example:

// ONLINE-RUNNER:browser;

// Note: Uncomment import lines during working with JSX Compiler.
// import React from 'react';
// import ReactDOM from 'react-dom';

const Row = () => {
  return (
    <p>Row</p>
  );
}

const App = () => {
  const size = 5;
  return (
    <div>
      {Array(size).fill().map((x, i) =>
        <Row key={i} />
      )}
    </div>
  );
}

const root = document.querySelector('#root');
ReactDOM.render(<App />, root);

References

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - loop inside JSX
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join