2
2

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 Hooksで複数のFormを扱う

Last updated at Posted at 2019-12-31
const Forms: React.FC = () => {
  const [inputTexts, setInputTexts] = useState({
    firstName: '', lastName: '', e_mail: '', password: ''
  })

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setInputTexts({ ...inputTexts, [e.target.name]: e.currentTarget.value })
  }

  return (
    <div>
      {Object.entries(inputTexts).map(([key, value]) =>
        return <input type="text" key={key} name={key} value={value} onChange={handleChange} />
      )}
    </div>
  )
}
  • stateを一括りにまとめる
  • Object.entriesで 各keyvalueを取得
  • inputタグのnamekeyを指定 → onChangeevent.target.namekeyを取得
2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?