Capítulo 13 de 39

Chapter 13: getValues

Core Idea

getValues(name?, config?) reads current form values on demand without subscribing or re-rendering — the non-reactive counterpart to watch.

Key Concepts

  • No argument: entire form values object.
  • String: value at that path.
  • String[]: array of values, one per path.
  • config.dirtyFields / config.touchedFields (7.63.0+): filter the returned object to only fields present in formState.dirtyFields/touchedFields.
  • Pre-mount behavior: before the initial render, getValues() returns defaultValues from useForm.

Code Examples

<input {...register("root.test1")} />
<input {...register("root.test2")} />
getValues()                 // { root: { test1: '', test2: '' } }
getValues("root")           // { test1: '', test2: '' }
getValues("root.test1")     // ''
getValues(["root.test1", "root.test2"])       // ['', '']
getValues(undefined, { dirtyFields: true })   // only dirty fields
  • What it demonstrates: the shape of nested field paths, and the dirty/touched filtering config.

Anti-patterns

  • Using getValues where a re-render on change is actually needed: it never triggers one — use watch/useWatch for that.

Key Takeaways

  1. getValues is the cheap, non-reactive read; reach for it inside event handlers, submit logic, or anywhere you don't want a subscription.
  2. config.dirtyFields/config.touchedFields (7.63.0+) let you pull just the subset of values the user actually changed/touched.

Connects To

  • useform-watch / usewatch: the reactive, subscribing equivalents.
  • useform-formstate: source of the dirtyFields/touchedFields the config option filters by.