setValue: (name: string, value: unknown, config?: Object) => void
This function allows you to dynamically set the value of a registered field and have the options to validate and update the form state. At the same time, it tries to avoid unnecessary rerender.
Props
Name | Type | Description | |
---|---|---|---|
name | string |
| |
value | unknown | The value for the field. This argument is required and can not be | |
options | shouldValidate | boolean |
|
| shouldDirty | boolean |
|
| shouldTouch | boolean | Whether to set the input itself to be touched.
|
Rules
Only the following conditions will trigger a re-render:
When an error is triggered or corrected by a value update
When setValue cause state update, such as dirty and touched.
It's recommended to target the field's name rather than make the second argument a nested object.
setValue('yourDetails.firstName', 'value'); // ✅ performant setValue('yourDetails', { firstName: 'value' }); // less performant register('nestedValue', { value: { test: 'data' } }); // register a nested value input setValue('nestedValue.test', 'updatedData'); // ❌ failed to find the relevant field setValue('nestedValue', { test: 'updatedData' } ); // ✅ setValue find input and update
It's recommended to register the input's name before invoking
setValue
. To update the entire Field Array, make sure theuseFieldArray
hook is being executed first.Important: use
replace
fromuseFieldArray
instead, update entire field array withsetValue
will be removed in the next major version.// you can update an entire Field Array, setValue('fieldArray', [{ test: '1' }, { test: '2' }]); // ✅ // you can setValue to a unregistered input setValue('notRegisteredInput', 'value'); // ✅ prefer to be registered // the following will register a single input (without register invoked) setValue('resultSingleNestedField', { test: '1', test2: '2' }); // 🤔 // with registered inputs, the setValue will update both inputs correctly. register('notRegisteredInput.test', '1') register('notRegisteredInput.test2', '2') setValue('notRegisteredInput', { test: '1', test2: '2' }); // ✅ sugar syntax to setValue twice
Examples
import { useForm } from "react-hook-form"; const App = () => { const { register, setValue } = useForm(); return ( <form> <input {...register("firstName")} /> <button type="button" onClick={() => setValue("firstName", "Bill")}> setValue </button> <button type="button" onClick={() => setValue("lastName", "firstName", { shouldValidate: true, shouldDirty: true }) } > setValue options </button> </form> ); };
import { useForm } from "react-hook-form"; const App = () => { const { register, setValue } = useForm({ firstName: '' }); return ( <form> <input {...register("firstName", { required: true })} /> <button onClick={() => setValue("firstName", "Bill")}> setValue </button> <button onClick={() => setValue("firstName", "Luo", { shouldValidate: true, shouldDirty: true }) } > setValue options </button> </form> ); };
import React from "react"; import { useForm } from "react-hook-form"; type FormValues = { string: string; number: number; object: { number: number; boolean: boolean; }; array: { string: string; boolean: boolean; }[]; }; export default function App() { const { setValue } = useForm<FormValues>(); setValue("string", "test"); // function setValue<"string", string>(name: "string", value: string, shouldValidate?: boolean | undefined): void setValue("number", 1); // function setValue<"number", number>(name: "number", value: number, shouldValidate?: boolean | undefined): void setValue("number", "error"); return <form />; }
Video
The following video tutorial demonstrates setValue
API in detail.
Thank you for your support
If you find React Hook Form to be useful in your project, please consider to star and support it.