kettanaito / react-advanced-form

Functional reactive forms. Multi-layer validation, custom styling, field grouping, reactive props, and much more.
https://redd.gitbook.io/react-advanced-form
MIT License
217 stars 24 forks source link

React.StrictMode throws error for all forms #412

Open JacobGalati opened 3 years ago

JacobGalati commented 3 years ago

Environment

What

When running a create-react-app with React.StrictMode (the default for React 17) an Uncaught Invariant Viloation error is thrown for all form fields.

Current behavior

An error is thrown for the first form field and the field does not function. Example:

Uncaught Invariant Violation: Cannot register field `userEmail`, 
the field with the provided name is already registered. 
Make sure the fields on the same level of `Form` or `Field.Group` have unique names.

Expected behavior

No error would be thrown and the field would function as expected.

Why

I think it is because strict mode renders components twice to detect code issues as mentioned here

How

Steps to reproduce the issue (ideally, link to the repository/sandbox). Here is a Sandbox of the error.

JacobGalati commented 3 years ago

To clarify this obviously is only an issue in development since strict mode has no effect on production.

kettanaito commented 3 years ago

Hey, @JacobGalati. I recommend putting your use case in an isolated reproduction. That may help you spot the issues.

I don't think React's strict mode has anything to do with the error (but it may). Perhaps you're indeed registering (rendering) two fields with the same name?

JacobGalati commented 3 years ago

Hey @kettanaito if you take a look at my sandbox I linked at the bottom of the issue you can see the errors. Commenting out makes the errors go away.

kettanaito commented 3 years ago

Thanks for providing the sandbox, @JacobGalati!

I don't have time capacity to maintain this library right now, so feel free to investigate on your own. I can dedicate some time to do a code review and merge the fix, but I wouldn't count on anything else.