0
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?

【入力した文字を保存するしくみ onChange={(e) => 関数}】

Last updated at Posted at 2025-07-07

onChangeとは、フォーム内のエレメント(要素)の内容が変更された時に起こイベント処理。入力された文字をリアルタイムで保存する。
検索やフォーム入力でよく使われる。

<input 
  type="text" 
  value={searchTerm} 
  onChange={(e) => setSearchTerm(e.target.value)}  // 入力された文字を保存!
/>  // ↑個々のset~はコードによって変わる。

e.target.valueで 今入力した文字を取得してsetSearchTermで 保存する!
type="text" → 「これはテキスト入力欄です」 という指定(これはほぼ固定)
value={searchTerm} → 入力欄の値として searchTerm を使う(状態管理)
onChange={(e) => setSearchTerm(e.target.value)} → 文字が入力されたら状態を変更する

0
0
1

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
0
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?