Capítulo 11 de 39

Chapter 11: getErrors

Core Idea

getErrors(name?) (7.86.0+) reads currently-stored errors without running validation, subscribing, or causing a re-render — the non-reactive counterpart to formState.errors.

Key Concepts

  • No argument: returns every stored error.
  • String: returns the error at a field, a parent path (may return a nested subtree), or a global error namespace (root, root.*, form, form.*); undefined if none.
  • String[]: returns one result per path, in order, missing entries as undefined.
  • Read-only: never mutate the returned object — use setError/clearErrors to change stored errors.

Code Examples

const allErrors = getErrors()
const userErrors = getErrors("user")
const [emailError, firstNameError] = getErrors(["email", "user.firstName"])
  • What it demonstrates: the three call shapes, reading errors on demand (e.g. inside an event handler) without subscribing the component to them.

Anti-patterns

  • Using getErrors where you need the UI to react to error changes: it never triggers a re-render — use formState.errors or useFormState for that.

Key Takeaways

  1. getErrors is to formState.errors what getValues is to watch — an on-demand, non-reactive read.
  2. Parent/namespace paths ("user", "root", "form") can return a nested subtree, not just a leaf error.

Connects To

  • useform-formstate / useformstate: the reactive equivalents.
  • useform-seterror / useform-clearerrors: how stored errors actually get written.