question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

errors and formState.errors from the useForm hooks are mutated

See original GitHub issue

If 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

  1. open the console
  2. click on the “last name” input
  3. click outside the input
  4. 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 image

Desktop

  • OS: Windows 10
  • Browser: Chrome
  • Version: 86

Related issues: #2893

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
bluebill1049commented, Nov 20, 2020
Screen Shot 2020-11-20 at 6 12 21 pm

updated 👍

2reactions
vzaidmancommented, Nov 16, 2020

also if you do something like:

React.useEffect(() => {
  setTimeout(() => {
    console.log("formState.errors", formState.errors);
  }, 1000);
}, [formState]);

you are not guaranteed to have the correct errors object. so it would be a good idea to do the following:

React.useEffect(() => {
  const currentErrors = {...formState.errors}
  setTimeout(() => {
    console.log("currentErrors", currentErrors);
  }, 1000);
}, [formState]);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found