react-hook-form custom errors disappear, or never clear, after setError
setError message flashes and then disappears on submitShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
The custom message appears and handleSubmit still runs onSubmit.setError was used as if it blocked submissionFix - 2
The 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
You call clearErrors("field") and the same error returns on the next validation.clearErrors was expected to re-validate or to fix the valueFix
- 1
What does the error look like?
setError message flashes and then disappears on submiterror 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" src2Fix
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
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 submit2Fix
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
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" src2Fix
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 rules3Verify
Fix the value and confirm errors.username is empty after trigger.
console.log(formState.errors.username);Confirmed by react-hook-form.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
A Zod refinement error does not appear or does not clear in React Hook Form
The refinement error never shows until every other field is valid
Three Zod and React Hook Form rules explain it.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix