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
propsの使い方|初心者から学ぶReact.js入門

propsの使い方

propsは、親コンポーネントから子コンポーネントにデータを渡すための仕組みです。文字列、数値、オブジェクト、配列など、任意のデータを渡すことができます。

受け取ったデータに対して処理をするようにコンポーネントを作っておけば、似たようなコンポーネントを量産せずにすみます。コンポーネントを再利用しやすく管理できるようになります。

propsの使用例

次の例は、親コンポーネントから子コンポーネントにpropsを渡して、子コンポーネントでその内容を表示させる例です。

別のコンポーネントに値を渡す書き方

// 親コンポーネント
const ParentComponent = () => {
	return (
		<div>
			<ChildComponent name="ちゃろぼ" age="20" />
		</div>
	);
};

別のコンポーネントから値を受け取る書き方

// 子コンポーネント
const ChildComponent = ({ name, age }) => {
	return (
		<div>
			<h1>私の名前は {name} です。</h1>
			<p>年齢は {age} 歳です。</p>
		</div>
	);
};

実際のJSXソースコード

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

// 親コンポーネント
const ParentComponent = () => {
	return (
		<div>
			<ChildComponent name="ちゃろぼ" age="20" />
		</div>
	);
};

// 子コンポーネント
const ChildComponent = ({ name, age }) => {
	return (
		<div>
			<h1>私の名前は {name} です。</h1>
			<p>年齢は {age} 歳です。</p>
		</div>
	);
};

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

実際にできあがるHTML

このコードを実行すると、次のようなHTMLになります。

<div>
	<h1>私の名前は ちゃろぼ です。</h1>
	<p>年齢は 20 歳です。</p>
</div>