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入門

イベント処理の書き方

イベント」とは、ユーザーがWebページの要素に対して行った操作のことです。例えば、ボタンをクリックしたり、テキストボックスに文字を入力したり、画像にカーソルを合わせたり。こうした行動がイベントと呼ばれます。

また、これらのイベントが生じたときに何か実行させる処理のことを「イベントハンドラ」と呼びます。

①基本の書き方

React.jsでのイベントハンドラの書き方はこちらです。

基本構文

<要素 onClick={関数}>…</要素>

イベント名キャメルケース(用語の意味)で書くのがポイントです。通常のHTMLでは「onclick」が正しい書き方ですが、JSX記法の場合は「onClick」が正しい書き方です。

実際の記述例

<button onClick={myFunction}>ボタン</button>

myFunction関数の部分は、自由な関数名で問題ありません。この例の場合は、ボタンをクリックしたときにmyFunctionの処理が実行されます。

複数のイベントを同時に書く例

下記のように、いくつかのイベントを同時に書くこともできます。

<button onClick={myFunction1} onMouseEnter={myFunction2} />ボタン>

関数名ではなく、アロー関数を直接書く例

簡単な処理であれば、わざわざ関数を作らなくとも、このようにアロー関数で書いたほうが楽な場合もあります。

<button onClick={() => alert("Hello, World!")}>ボタン</button>

②イベントハンドラへの引数の渡し方

イベントハンドラには、下記のように引数を渡すこともできます。

<button onClick={myFunction(event)}>ボタン</button>