wsmd/react-use-form-state

Question: how would one handle "dynamically added fields" ?

gregfenton opened this issue · 2 comments

If there is a way to support dynamically added fields (e.g. a form with an "add extra field" button) with this hook, it would be a good example to have. My brief reading of the docs, it seems that the fields are to be declared at the time of calling useFormState(), and I don't see a method in Form State or Input Types for adding new fields.

wsmd commented

Hi @gregfenton – dynamic fields are registered as soon as they are rendered. The values declared at the time of calling useFormState() are the default/initial values of the inputs, not the actual declaration of the form inputs.

This example demonstrate how to track dynamically added fields in your form state: https://codesandbox.io/s/ufs-dynamic-fields-x5olp

I hope this helps!

Very cool. Took me a few minutes to realize how the dynamic fields are getting linked to the formState (though typed as "fromState"): via the text() on the .

If this isn't already documented, such a straightforward example would be great to have added!

Cheers and thanks!