1
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 1 year has passed since last update.

問題のコード

export const DataTable = ({data}) => {
    return (
        <div>
            {data.map((col) => {
                <span>{col.key}</span>
                <span>{col.value}</span>
            })}
        </div>
    )
};

上記のdata.map()内でエラーが表示されました。

原因

ReactのJSXは、常に一つの要素を返すというルールがあります。
例として、下記のようなコードは題名と同様のエラーが発生します。

export const sample = () => {
    return (
        <div>あいうえお</div>
        <div>はひふへほ</div>
    )
};

data.map等の処理は、JSX内に記述していても1つの要素を返すルールを満たしている必要があるようです。
下記のコードに書き換えたところエラー表示が消えました。

export const DataTable = ({data}) => {
    return (
        <div>
            {data.map((col) => {
                <>
                    <span>{col.key}</span>
                    <span>{col.value}</span>
                </>
            })}
        </div>
    )
};

最後に

慣れてきたらJSXやReactが裏でどのように動いているのかも勉強してみたいと思います。

1
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
1
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?