form.update() silently wipes onSubmit and other options - need way to update defaultValues safely #1988
Unanswered
derekbking
asked this question in
Ideas
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.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to share my use case of having a component that automatically saves via a debounce mechanism.
Use Case: Auto-Save Form
I have a form that auto-saves when the user stops typing for 2 seconds. After a successful save, I need to update
defaultValuesso that:isDefaultValuebecomestrue(my button shows "Saved")The Problem: Race Condition Window
There's an important window where the mutation is pending and users can still modify fields:
If you call
reset()inonSuccess, the user's changes to field B get wiped. I want to callresetso my button can show "Saved" and detect whether the form is dirty. If the user makes changes during this window, the debounce should fire again to save those changes.Ideally I want to load server data when the form is initialized and then keep the state client-only. I know there are discussions about how the client should not own state, but this feels like the simplest way to implement auto-saving. I don't have many users accessing this form simultaneously, so conflicts aren't a concern.
What I Want
I want to call
resetwith apreserveValuesoption to keep the current values but updatedefaultValues:What I Was Doing (Broken)
Previously I was using
form.update()withdefaultValues:This silently broke my form. The first save worked, but all subsequent saves failed because
update()replaces the entire options object:What I Should Do (Workaround)
The workaround is to spread the existing options:
This works but took a bit to debug. The failure is completely silent
handleSubmitgets called, passes all validation, then does nothing becauseonSubmitis undefined.Suggestions
1.
update()should merge options instead of replacing. I completely recognize that merging could be just as confusing this was just the assumption I made about it.2. Add
preserveValuesoption toreset()This would update
defaultValuesand recalculateisDefaultValuewithout touching the current form values.Maybe the
update()behavior is expected, but I wanted to share how I misunderstood the API. I spent a long time debugging why my second save silently failed. I'm wondering if the behavior should be different, or at minimum documented more clearly.Thanks for the great library!
Beta Was this translation helpful? Give feedback.
All reactions