</> getValues: (payload?: string | string[]) => Object
An optimized helper for reading form values. The difference between watch and getValues is that getValues will not trigger re-renders or subscribe to input changes.
Props
| Name | Type | Description |
|---|---|---|
fieldNames | undefined | Returns the entire form values. |
string | Gets the value at path of the form values. | |
array | Returns an array of the value at path of the form values. | |
config | dirtyFields | Returns only dirty fields |
touchedFields | Return only touchedFields |
Examples:
The example below shows what to expect when you invoke getValues method.
<input {...register("root.test1")} /><input {...register("root.test2")} />
| Name | Output |
|---|---|
getValues() | { root: { test1: '', test2: ''} } |
getValues("root") | { test1: '', test2: ''} |
getValues("root.firstName") | '' |
getValues(["yourDetails.lastName"]) | [''] |
getValues(undefined, { dirtyFields: true }) | { root: { test1: '', test2: ''} } |
getValues(undefined, { touchedFields: true }) | { root: { test1: '', test2: ''} } |
RULES
- It will return
defaultValuesfromuseFormbefore the initial render.
Examples:
import { useForm } from "react-hook-form"type FormInputs = {test: stringtest1: string}export default function App() {const { register, getValues } = useForm<FormInputs>()return (<form><input {...register("test")} /><input {...register("test1")} /><buttontype="button"onClick={() => {const values = getValues() // { test: "test-input", test1: "test1-input" }const singleValue = getValues("test") // "test-input"const multipleValues = getValues(["test", "test1"]) // ["test-input", "test1-input"]}}>Get Values</button></form>)}
Thank you for your support
If you find React Hook Form to be useful in your project, please consider to star and support it.