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

More than 1 year has passed since last update.

Reactでinputタグに入力した文字列を取得する

Posted at

方法

Reactでinputタグに入力した文字列を取得するにはuseRefを使用します。

具体的なコード

下記コードでは「文字列を取得」ボタンを押すとブラウザのコンソールにinputタグに入力した文字列が出力されるコードです。

App.js
import { useState, useRef } from "react";

function App() {

  const textRef = useRef();

  const getText = () =>{
    console.log(textRef.current.value);
  };

  return (
    <>
      <input type="text" ref={textRef}></input>
      <button onClick={getText}>文字列を取得</button>
    </>
  );
}

export default App;

注意点

下記の記載を忘れないようにしましょう。

・inputタグのref
・buttonタグのonClick

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