4

When using React Hook Form, while I understand that the "defaultValues" is used to set the form field values on initial form load.

However, let's say there is a dynamic form with dependent fields e.g. if Dropdown Field 1 is set to "ABC", then Field 2 should be set to "ABC 1 Updated" To handle such use cases, is it possible to "watch" Field 1 and based on it's value, update the "defaultValues" with Field 2 then set to "ABC 1 Updated"

or is it not recommended to update "defaultValues" after the initial form load.

I also see "values" in the below documentation, but haven't seen it's usage much. Other option is doing "setValue". Not sure what is the recommended way of handling side effects with a dynamic form with dependent fields.

https://react-hook-form.com/docs/useform#defaultValues

2 Answers 2

4

You should use values if it's dependent on the return of an async function or the field is dynamic. Use defaultValues if the fields are static.

You should never use setValue for updating a field array. This could cause problems in rerendering components. Just put it from values.

values: asynFn ? { ...asyncFn } : defaultValues
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, it's not recommended to update "defaultValues".

You can use reset or setValue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.