Closed jfbloom22 closed 6 years ago
Hey @jfbloom22, It's hard to understand the issue like this, can you please send a code sandbox link with your use case. It'll be really helpful. Thanks
I put together a sandbox based on your Simple Form With FormGenerator
example. https://codesandbox.io/s/n46owpxr2m
You should see it log setForm twice.
Putting this together helped me track down the culprit. In the render statement I am creating a new fieldConfig object (line 103 in the sandbox). I am doing this because I have a form utility file with some re-usable form configurations. In the component I am adding a few additional form fields.
Solution: I simply moved the creation of the new fieldConfig out of the render statement. Now everything works great!
Cool, yes that's the solution.
You must keep your fieldConfig
out of the render function to prevent unnecessary control generation , if you're fieldConfig is not static then you can keep it in a state variable.
Never change the reference of fieldConfig
, until needed.
The form works great until I do a setState() somewhere which triggers a render. Then the form appears to completely loose it's configuration as if there were 0 fields. I noticed that the
form
passed fromonMount
the second and following times had no fields. As you can see in the code below I am setting the form to state, then once it is defined on state, I use that one to set back tothis.userForm
rather than what is passed fromonMont
. This is working, but I am not sure if this is the best way or maybe I have something setup wrong?