13
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Reactでループする

Last updated at Posted at 2017-08-03

Code : https://codepen.io/mo4_9/pen/EvgbZM

事前に配列を生成する

class LoopComponent01 extends React.Component {
  render() {
    const items = [];
    for (let i = 0; i < 10; i++) {
      items.push(
        <div class="item">item--{i}</div>
      )
    }
    return (
      <div id="list">
        {items}
      </div>
    )
  }
}

mapを使用する

class LoopComponent02 extends React.Component {
  render() {
    const items = ['Sun', 'Mon', 'Tue', 'Wed'];

    return (
      <ul>
        { items.map(d => {
          return <li>{d}</li>
        }) }
      </ul>
    );
  }
}

map,keyを使用する

keyは差分計算を効率化するため(keyの重複には注意)

class LoopComponent03 extends React.Component {
  render() {
    const items = ['Sun', 'Mon', 'Tue', 'Wed'];

    return (
      <ul>
        { items.map((d, idx) => {
          return <li key={idx}>{idx} : {d}</li>
        }) }
      </ul>
    );
  }
}
13
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?