Deprecated: Smarty::_getTemplateId(): Implicitly marking parameter $template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/Smarty.class.php on line 1048

Deprecated: Smarty_Internal_Data::getTemplateVars(): Implicitly marking parameter $_ptr as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_data.php on line 193

Deprecated: Smarty_Internal_Data::_mergeVars(): Implicitly marking parameter $data as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_data.php on line 203

Deprecated: Smarty_Internal_Template::__construct(): Implicitly marking parameter $_parent as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_template.php on line 149

Deprecated: Smarty_Resource::source(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_resource.php on line 175

Deprecated: Smarty_Resource::source(): Implicitly marking parameter $smarty as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_resource.php on line 175

Deprecated: Smarty_Resource::populate(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_resource.php on line 199

Deprecated: Smarty_Template_Source::load(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_template_source.php on line 158

Deprecated: Smarty_Template_Source::load(): Implicitly marking parameter $smarty as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_template_source.php on line 158

Deprecated: Smarty_Internal_Resource_File::populate(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_resource_file.php on line 28

Deprecated: Smarty_Internal_Resource_File::buildFilepath(): Implicitly marking parameter $_template as nullable is deprecated, the explicit nullable type must be used instead in /export/sd206/www/jp/r/e/gmoserver/1/0/sd0297310/charobo.com/libs/sysplugins/smarty_internal_resource_file.php on line 101
【JSXのルール】returnで返すときのルート要素は1つにする|初心者から学ぶReact.js入門

returnで返すときのルート要素は1つにする

Reactでコンポーネントのreturnで要素を取り扱うときには、そのルート要素は1つでないといけません。

エラーになる要素の渡し方

このように、2つ以上の要素を一挙にreturnしようとするとエラーになります。

import React from "react";
import ReactDOM from "react-dom";

const App = () => {
	return (
		<div>…</div>
		<div>…</div>
		<div>…</div>
	);
}

ReactDOM.render(
	<App />,
	document.getElementById('root')
);

エラーにならない要素の渡し方

要素のルートが1つであればエラーにならないので、何らかの要素で囲いましょう。

①HTML要素で囲う

import React from "react";
import ReactDOM from "react-dom";

const App = () => {
	return (
		<div>
			<div>…</div>
			<div>…</div>
			<div>…</div>
		</div>
	);
}

ReactDOM.render(
	<App />,
	document.getElementById('root')
);

②Fragmentで囲う

Fragmentは、実際にはレンダリングされません。余計な要素を画面表示の際に増やしたくない場合はこのような記述をします。

import React from "react";
import ReactDOM from "react-dom";
import { Fragment } from "react";

const App = () => {
	return (
		<React.Fragment>
			<div>…</div>
			<div>…</div>
			<div>…</div>
		</React.Fragment>
	);
}

ReactDOM.render(
	<App />,
	document.getElementById('root')
);

Fragmentimportする必要があるので、要注意です。

③省略記法(空タグ)で囲う

空タグで囲うこともできます。Fragmentと同じ働きをします。

import React from "react";
import ReactDOM from "react-dom";

const App = () => {
	return (
		<>
			<div>…</div>
			<div>…</div>
			<div>…</div>
		</>
	);
}

ReactDOM.render(
	<App />,
	document.getElementById('root')
);

この書き方だと、Fragmentimportする必要がないので楽な上に、記法もシンプルでわかりやすいです。