errors and formState.errors from the useForm hooks are mutated
See original GitHub issueIf you use errors
or formState.errors
from useForm
, you receive a persistent error object that can be modified later.
It won’t trigger useEffect in code like this, because both errors
objects are always the same obejct:
React.useEffect(() => {
console.log("errors", errors);
}, [errors]);
React.useEffect(() => {
console.log("formState.errors", formState.errors);
}, [formState.errors]);
But may also be buggy in cases where this object is used after the fact, not expecting it has been mutated.
Codesandbox link https://codesandbox.io/s/react-hook-form-useform-template-forked-fmljf?file=/src/index.js
To Reproduce
- open the console
- click on the “last name” input
- click outside the input
- you will see that the component has been updated with the new error, but the
useEffect
hooks in the code were not triggered.
Expected behavior I expected to have the error object to be able to work with it. For example, use it for analytics. It should also be a unique, unmutable object because else I might send a mutated, later version of it.
Screenshots
Desktop
- OS: Windows 10
- Browser: Chrome
- Version: 86
Related issues: #2893
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
useForm - FormState
Indicate the form was successfully submitted without any Promise rejection or Error been thrown within the handleSubmit callback. isSubmitting, boolean, true if ...
Read more >React hook form show errrors block when state is passed
Since errors is a object .length won't work for it. But you can use Object.keys(errors).length > 0 to dynamic display the error block....
Read more >React Hook Form - A Complete Guide
React Hook Form is one such library that helps to manage complex forms. ... You can get the error object from useForm and...
Read more >useForm
All of React Hook Form's features are supported and you can use all of the React ... {errors.title && <span>This field is required</span>}...
Read more >react-hook-form useForm TypeScript Examples
This page shows TypeScript code examples of react-hook-form useForm. ... isSubmitting, isSubmitted, errors } = formState; async function onSubmit(payload: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
updated 👍
also if you do something like:
you are not guaranteed to have the correct errors object. so it would be a good idea to do the following: