1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Reactで入力された値を保持させたい

Last updated at Posted at 2021-09-23

inputに入力された値を保持、表示させる

onChangeイベントとuseStateを利用してinputタグに入力された値を表示させます。

まずはstateを管理するための記述を書きます。

// CDNで React を利用する場合はReact.useState()と記述します。
const [name, setName] = React.useState('〇〇');

次に現在の状態をセットする関数の定義

const handleOnChange = (event) = setName(event.target.value);

レンダリングするDOMを定義

return (
  <div>
    // changeイベントが発生した時にnameの値をセットする関数を呼び出す
    <input type="text" onChange={handleOnChange} />
    <p>こんにちは{name}さん</p>
  </div>
);
1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?