0
0

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 3 years have passed since last update.

#React で Component を複数個配置した時のエラー と return を囲む空タグの意味 / Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag.

0
Last updated at Posted at 2020-03-17

コード

import React from 'react';

import CircularProgress from '@material-ui/core/CircularProgress'

function App() {
  return (
    <CircularProgress />
    <CircularProgress />
    <CircularProgress />
  );
}

export default App;

エラー

image

解決

タグで囲めば良いらしい。

import React from 'react';

import CircularProgress from '@material-ui/core/CircularProgress'

function App() {
  return (
    <div>
      <CircularProgress />
      <CircularProgress />
      <CircularProgress />
    </div>
  );
}

export default App;

Fragment

より正しそうな書き方

import React from 'react';

import CircularProgress from '@material-ui/core/CircularProgress'

function App() {
  return (
    <React.Fragment>
      <CircularProgress />
      <CircularProgress />
      <CircularProgress />
  </React.Fragment>
  );
}

export default App;

空のタグ

エラー回避のハックだろうか? 空のタグでも良いみたいだ。

import React from 'react';

import CircularProgress from '@material-ui/core/CircularProgress'

function App() {
  return (
    <>
      <CircularProgress />
      <CircularProgress />
      <CircularProgress />
    </>
  );
}

export default App;

と思ったら Fragment の短縮記法らしい。

image

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?