getErrors
Read current form errors without subscribing to re-renders.
</> getErrors: UseFormGetErrors Since v7.86.0
getErrors reads the errors currently stored in the form without running validation, subscribing to error changes, or triggering re-renders. Use formState.errors or useFormState for reactive error UI.
Props
| Name | Type | Description |
|---|---|---|
name | undefined | Returns all currently stored errors. |
| string | Returns the error at a field, parent, or global error path (root, root.*, form, or form.*), or undefined. Parent paths may return a nested error subtree. | |
| string[] | Returns one result for each path in the same order. Missing errors remain undefined. |
RULES
- Treat returned errors as read-only. Use
setErrorandclearErrorsto update the form's stored errors.
Example
import { useForm } from "react-hook-form"type FormInputs = {email: stringuser: {firstName: string}}export default function App() {const { register, getErrors } = useForm<FormInputs>({mode: "onChange",})const readErrors = () => {const allErrors = getErrors()const userErrors = getErrors("user")const [emailError, firstNameError] = getErrors(["email", "user.firstName"])console.log({allErrors,userErrors,emailError,firstNameError,})}return (<form><input {...register("email", { required: true })} /><input {...register("user.firstName", { required: true })} /><button type="button" onClick={readErrors}>Read errors</button></form>)}
Tips
-
Use
getErrorsin event handlers and effects where you need the current errors but do not want a re-render. For example, in a multi-step form, find which steps have errors after validation:const onNext = async () => {await trigger()const [account, address] = getErrors(["account", "address"])setStepsWithErrors({ account: !!account, address: !!address })} -
Do not render from
getErrors. The value is not reactive, so the UI does not update when errors change. UseformState.errorsorErrorMessagefor UI.