0
1

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 Hook Formのresetは「Formを空にする」わけではない

Posted at

React Hook Formのresetは「Formを空の状態にする」という理解でいましたが勘違いしていました。

"use client";

import { useForm, SubmitHandler } from "react-hook-form";

export default function Home() {
  const { register, reset } = useForm({
    defaultValues: {
      hoge: "test",
    },
  });

  return (
    <>
      <input {...register("hoge")} />
      <button
        onClick={() => {
          reset();
        }}
      >
        リセット
      </button>
    </>
  );
}

上記は、defaultValuesでhogeの初期値に「test」を設定しています。
hogeの値を変更後、リセットボタンを押下するとhogeの値は空ではなく「test」になりました。

値を空にするにはreset({ hoge: "" })とするか、setValueを使用してsetValue("hoge", "")とすれば可能です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?