Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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: Infinite Loop: Submitting FormContext with Nested FormContext causes repeated error rendering #11799

Closed
1 task done
ifozest opened this issue Apr 19, 2024 · 1 comment

Comments

@ifozest
Copy link

ifozest commented Apr 19, 2024

Version Number

7.51.3

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/react-hook-form-infinite-loop-3plm39

Steps to reproduce

Hi,

I'm encountering an issue with nested FormContext components in my React application that uses the approach outlined here for handling tabbed forms: https://react-hook-form.com/faqs#Howtoworkwithmodalortabforms
My application has a complex form composed of multiple sub-forms. I want to trigger validation on all visible forms simultaneously to display any built-in errors (like required) and custom errors based on business logic.

I've isolated the problem to the rendering of formState.errors within a nested FormContext. This is causing an infinite loop rendering error when submitting the form.

I made a codesanbox with minimum reproducible code. Note that deleting line 18 in InsideForm component will resolve the issue, but in this case I won't be able to display any errors on the screen.

I noticed that I can call handleSubmit directly on the useForm instance to avoid this issue.

  1. Go to sandbox link
  2. Open console
  3. Click on the button "Hit me"
  4. No state change console logs, Observe the message "Page unresponsive"; page is dead

Expected behaviour

No infinite lopp

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@ifozest
Copy link
Author

ifozest commented Apr 19, 2024

Well, seems like putting formContext object in useEffect deps was not a good idea after all

@react-hook-form react-hook-form locked and limited conversation to collaborators May 5, 2024
@bluebill1049 bluebill1049 converted this issue into discussion #11861 May 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant