0
4

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] JSX内で直接for文やif文を使う

Last updated at Posted at 2019-02-23

したかったこと

renderメソッドで返すJSXの外側でfor文やif文や定義すると、return処理が増えたりして煩雑になってしまうので、JSXの中で直接for文やif文を呼び出したかった。

採用した作戦

即時関数を使う
参考にさせていただいたサイト - Qiita

コード

ScoutArticleSelector.jsx
render() {
    const articles = this.state.articles;
    return (
      {(() => {
          for(let i=0; i < articles.length; i++) {
            const label = articles[i].label;
            if (articles[i].value == this.props.target_article_id) {
              return(
                <div>
                  ...
                </div>
              )
            }
          }
      })()}
   )
}

まだ汚い感じがしますが…

0
4
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
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?