Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

react-hook-form custom errors disappear, or never clear, after setError

ErrorsetError message flashes and then disappears on submit

Short answer

setError only writes an error; it adds no rule. handleSubmit validates again and drops errors that no rule backs, and a registered field's manual error goes once its own rules pass. clearErrors removes errors but never re-validates. Put the check in a validation rule, or use root.serverError for server failures.

  • Applies to react-hook-form 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the errors object right after the call and after the next submit, to see which step removes the error.

    setError('username', { type: 'custom', message: 'Name is taken' });
    console.log(formState.errors.username);
  2. 2Match what you see, then jump to its fix

    1. 1The custom message appears and handleSubmit still runs onSubmit.setError was used as if it blocked submissionFix
    2. 2The failure message from the API shows once, then goes away when the user submits again.A server error is set on a field and wiped on the next submitFix
    3. 3You call clearErrors("field") and the same error returns on the next validation.clearErrors was expected to re-validate or to fix the valueFix

What does the error look like?

  • setError message flashes and then disappears on submit
  • error stays after the user fixes the field

Cause 1 of 3

setError was used as if it blocked submission

1Check

The setError docs say it does not add a validation rule: handleSubmit validates again and clears errors that no rule backs. A password rule or an async check must be a rule.

grep -rn "setError" src

2Fix

Move the check into register as an async validate, so the form really fails on it.

register('username', {
  validate: async (value) =>
    (await isNameFree(value)) || 'Name is taken',
})

3Verify

Submit with a taken name; onSubmit must not run and the message must stay.

console.log(formState.errors.username?.message);

Confirmed by react-hook-form.com

Did this fix it?

Cause 2 of 3

A server error is set on a field and wiped on the next submit

1Check

Root errors do not persist across submissions, and a registered field's manual error is dropped once the field passes its own rules.

grep -rn "catch" src | grep -i submit

2Fix

Catch the error in onSubmit and set a root error with a key such as root.serverError. The handleSubmit docs recommend this because errors thrown in onSubmit are not swallowed.

const onSubmit = async (data) => {
  try {
    await save(data);
  } catch (e) {
    setError('root.serverError', { type: 'server', message: e.message });
  }
};

3Verify

Cause a server failure and confirm errors.root.serverError holds the message.

console.log(formState.errors.root?.serverError);

Confirmed by react-hook-form.com

Did this fix it?

Cause 3 of 3

clearErrors was expected to re-validate or to fix the value

1Check

clearErrors changes neither the rules nor formState.isValid, and does not re-validate. A field that is still invalid shows its error again.

grep -rn "clearErrors" src

2Fix

Correct the value or the rule first, then run trigger on the field to re-check it. Use clearErrors only for errors you set by hand.

clearErrors('username');   // remove the manual error
await trigger('username'); // re-run the field's rules

3Verify

Fix the value and confirm errors.username is empty after trigger.

console.log(formState.errors.username);

Confirmed by react-hook-form.com

Did this fix it?

How do I stop it coming back?

  • Treat setError as display only; real checks belong in rules or the schema.
  • Use root.serverError for failures that do not belong to a single field.
  • Avoid field names that collide with the error object: type, root, ref, types, message and form.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.