Languages
[Edit]
PL

React - pętla wewnątrz JSX

0 points
Created by:
Krzysiek
651

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 while 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

Alternative titles

  1. React - pętla wewnątrz znaczników JSX
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 (PL)

React - pętla wewnątrz 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