Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue: useFieldArray and getFieldState causes react console warnings #11834

Open
1 task done
axel-havukangas-tt opened this issue Apr 29, 2024 · 4 comments
Open
1 task done
Labels
question Further information is requested

Comments

@axel-havukangas-tt
Copy link

axel-havukangas-tt commented Apr 29, 2024

Version Number

7.51.3

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/nervous-pateu-v5t8t2

Steps to reproduce

  1. Go to codesandbox
  2. Click on "Add cat"
  3. Check web console for preview
  4. See error

Also, the render count increases a lot more than expected.

Expected behaviour

While the form works from the user perspective, I would expect it do so without producing React warnings.

What browsers are you seeing the problem on?

Chrome, Safari

Relevant log output

Warning: Cannot update a component (`App`) while rendering a different component (`Cats`). To locate the bad setState() call inside `Cats`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at Cats (https://v5t8t2.csb.app/src/App.js:48:40)
    at form
    at exports.FormProvider (https://v5t8t2.csb.app/node_modules/react-hook-form/dist/index.cjs.js:1:24227)
    at div
    at App (https://v5t8t2.csb.app/src/App.js:23:46)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@axel-havukangas-tt
Copy link
Author

Might be related to the issue described in https://github.com/orgs/react-hook-form/discussions/10964#discussioncomment-9113909.

@axel-havukangas-tt
Copy link
Author

axel-havukangas-tt commented May 3, 2024

Investigated this a bit more, and noticed that the issue seems to be related to passing the whole formState to getFieldState. Separately subscribing to changes to appropriate formState fields as described in https://react-hook-form.com/docs/useform/getfieldstate seems to work around the issue.

So in the example it would look like this

function Validation() {
  const {
    getFieldState,
    formState: { errors },
  } = useFormContext();
  const { invalid } = getFieldState("cats");

  return invalid === true ? "BAD CATS" : "GOOD CATS";
}

@sim391calado
Copy link

I still have this problem in 7.51.3

@axel-havukangas-tt
Copy link
Author

@sim391calado That was actually the version I used as well, not sure why the issue template generated an old version. Fixed that now.

Did removing formState from the getFieldState, and replacing it with manual subscription of formState fields help in your case?

@bluebill1049 bluebill1049 added the question Further information is requested label May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants