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
コンポーネントの基礎|初心者から学ぶReact.js入門

コンポーネントの基礎

コンポーネントは、再利用可能なコードの断片であり、UI(ユーザーインターフェース:見た目)の特定の部分です。HTML、CSS、JavaScriptの組み合わせで記述され、再利用可能なコードのブロックとして機能します。そして、HTML要素を返します。

コンポーネントは、アプリケーションのロジックとUIを分離するのに役立ち、アプリケーションの開発と保守を容易にします。

HTMLでいうヘッダー、フッターなどをイメージしてもらうと良いでしょう。

コンポーネントの2つの作り方

クラスベースのコンポーネントJavaScriptのクラスを使って作る
状態を保持しておいて、その状態を使って複雑な処理を書くことができる
関数ベースのコンポーネントJavaScriptの関数を使って作る

クラスベースのコンポーネントの例

class HelloWorld extends React.Component {
	render() {
		return (
			<div>
				Hello, World!
			</div>
		);
	}
}

関数ベースのコンポーネントの例

const HelloWorld = () => {
	return (
		<div>
			Hello, World!
		</div>
	);
};

コンポーネントは再利用できる

作ったコンポーネントは再利用可能で、組み合わせて複雑なユーザーインターフェイスを作成できます。また、他の人が作ったコンポーネントを導入して使うこともできます。

1度作ったコンポーネントを使う例

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

const Button = () => {
	return (
		<button onClick={() => alert("ボタンが押されました")}>ボタン</button>
	);
};

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

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

コンポーネントを再利用しないと面倒になる

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

const App = () => {
	return (
		<div>
			<button onClick={() => alert("ボタンが押されました")}>ボタン</button>
			<button onClick={() => alert("ボタンが押されました")}>ボタン</button>
			<button onClick={() => alert("ボタンが押されました")}>ボタン</button>
			<button onClick={() => alert("ボタンが押されました")}>ボタン</button>
			<button onClick={() => alert("ボタンが押されました")}>ボタン</button>
		</div>
	);
};

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