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?

More than 1 year has passed since last update.

react-hook-formとuseRefを併用したい

Last updated at Posted at 2023-01-03

経緯

RHFはinputにrefを渡す必要があるが、独自にrefを渡したい場合、1つのコンポーネントに複数のrefを渡す必要があるので困っていた。

結論

公式ドキュメントに書いてあった

解説

ポイントは2箇所

1.refはコールバックにできた

<input {...rest} name="firstName" ref={(e) => {
	ref(e)
	firstNameRef.current = e // you can still assign to ref
}} />

2.nullとのUnionにすることで、currentプロパティがreadonlyではなくなる。

const firstNameRef = useRef<HTMLInputElement | null>(null);
0
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
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?