1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React学習ログ #4|イベントハンドラ

Last updated at Posted at 2025-12-30

前回:React学習ログ #3|データの表示・条件判定・リストのレンダー

イベントに応答する

チュートリアルリンク

コンポーネントの中でイベントハンドラ関数を宣言をしてみます。

src/App.jsx
import './App.css'

// ボタン用コンポーネント
function MyButton() {
  // アラート用コンポーネント
  function handleClick() {
    alert('You clicked me!');
  }

  return (
    <button onClick={handleClick}>
      Click me
    </button>
  );
}

export default function App() {
  return (
    <div>
      <MyButton />
    </div>
  );
}

解説

  • handleClickというアラートを出すコンポーネントをMyButton内に定義します
  • MyButtonはボタンのコンポーネントで、用意しているボタンにはonClickhandleClickを設定しています
  • AppではMyButtonを呼び出しています

実際の画面の動き

画面収録-2025-12-30-20.56.25.gif

次回

画面の更新を行います。
React学習ログ #5|画面の更新

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?