Preserving formState upon useFieldArray's remove() function #13148
Unanswered
sahajshrestha
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi team,
While using useFieldArray's remove() feature to remove certain elements from our formState array, we found out that it will result on the whole form being marked dirty. I also saw more discussion threads about this topic, and the general suggestion was to create our own logic to preserve the dirty state.
My approach is:
i) Keep track of the dirty states, indices, etc
ii) Use remove() to shrink the formState
iii) Reset the formState and umark the form as dirty
iv) Manually mark the field as dirty upon the dirtyState indices calculation.
This approach worked perfectly until we had the functionality to expand/ grow the formState once. Now, we have the product requirement to expland/ grow and shrink the formState as many times as the user wants - i'm using prepend+append for the formState to grow and remove to shrink. The only issue is, when manually marking the field as dirty, setDirty:true will not work if the current value matches the previous value in the field, and it does not make sense to update the value. I had initially had a workaround by introducing a new field to keep track of dirty field, but since the users should have the ability to expand and shrink the formState infinite times, it does not make sense for me to introduce a lot of fields to keep track of dirty fields.
I was wondering what is the workaround for this case, or if there is built-in functionality to overcome this.
Beta Was this translation helpful? Give feedback.
All reactions