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

useEffectの使い方

React.jsのuseEffectは、コンポーネントがレンダリングされた後、または状態やプロパティが変更された後に実行されます。

「副作用」と解説されているサイトや書籍が多くあります。なぜuseEffectが副作用といわれるのか一言でいうと、薬を飲んだときに副作用が現れることがあるように、何かが起きたときに起こすことを書くのがuseEffectだからです。

useEffectの使い方

useEffectを使う準備

useEffectを使うときは、まず下記を書きます。フックを使うならそのフックを必ずimportすると覚えておきましょう。

import { useState } from "react";

useEffectuseStateと一緒に使うことが多く、フックを2つ使う場合はこちらのように書きます。

import { useState } from "react";
import { useEffect } from "react";

こちらのように1行にまとめて書くこともできます。

import { useState, useEffect } from "react";

useEffectの基本構文

useEffectの基本構文がこちら。

useEffect(() => {
	// 何か処理を実行する
}, [状態やプロパティ]);

[状態やプロパティ]は配列で、この配列で指定した状態やプロパティが変更された場合に、useEffectが実行されます。[state, props]のように複数を指定できます。

配列が空の場合は、コンポーネントがレンダリングされる度に実行されます。

useEffectを使う例

まず、ボタンを押すたびに数値がカウントアップされていく例から。

import React from "react";
import ReactDOM from "react-dom";
import { useState, useEffect } from "react";

const Counter = () => {
	const [count, setCount] = useState(0);

	useEffect(() => {
		document.getElementById("output").innerText = count;
	}, [count]);

	return (
		<div>
			<button onClick={() => setCount(count + 1)}>カウントアップ</button>
			<div id="output"></div>
		</div>
	);
};

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

続いて、useEffectに渡す配列を空にすると、ボタンを押してもカウントアップされず0のままです。

import React from "react";
import ReactDOM from "react-dom";
import { useState, useEffect } from "react";

const Counter = () => {
	const [count, setCount] = useState(0);

	useEffect(() => {
		document.getElementById("output").innerText = count;
	}, []);

	return (
		<div>
			<button onClick={() => setCount(count + 1)}>カウントアップ</button>
			<div id="output"></div>
		</div>
	);
};

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

useEffectは、配列に何も指定しないと、レンダリングが行われたときに実行されるので、そのレンダリングのタイミングが初回のページ読込のタイミングとうわけです。