Roberto Conte Rosito


How to Use setValue with React Hook Form

My dev notes about how to use setValue with React Hook Form.

Following the official documentation, you can set a value with React Hook Form using the setValue method to change it programmatically like this:

1const { setValue } = useForm();
2...
3setValue("firstName", "bill");

But this is wrong!

You have to use the setValue method to change the value of a field programmatically, but only by using the useFormContext hook instead of the useForm hook:

1const { setValue } = useFormContext();
2...
3// Somewhere inside the form
4setValue("firstName", "bill");

Remember: the first one is used during form initialization, the second one is used inside the form.

comments powered by Disqus