Form options are stale after submit #1051
Send to my agentDescribe the bug#
I want to disable the submit button after the submission. I compare options.defaultValues and values from the store to determine whether they are equal. However, the next render after submission contains stale options. This happens likely because of the useIsomorphicLayoutEffect that updates the options here. I wonder maybe the formApi.update can be called during the render
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/elated-brahmagupta-sj6mvk
Steps to reproduce#
- Type "Johny" in the input
- Click the submit button
- Click the submit button again
Expected behavior#
As a user, I expect that the submit button is disabled after the first click, but it becomes disabled only after the second click
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: Windows
- Browser: Chrome
TanStack Form adapter#
react-form
TanStack Form version#
v0.39.0
TypeScript version#
v4.4.4
Additional context#
If the useQuery is replaced with a useState then everything works as expected.
crutchcorn(GitHub profile, opens in a new tab) added labelstype: bug
While it's not a fix itself, but a workaround for this specific case, I fixed the issue by calling
formApi.reset()inonSubmit, looks legit enoughI would be very interested in this feature. We need a "You have unsaved changes" warning in a rather complex form, and for that, the form's initial state as per
formApi.resetserves as a baseline validation.For what it's worth, we don't have to do the comparison ourselves. I would be happy if we could get some variation of
isFieldDirtythat respects the initial state. But having access to the initial form state outside of that field meta would also be useful in the long run. I just don't know the inner workings of this library well enough to judge which is easier.LeCarbonator(GitHub profile, opens in a new tab) added labelsv1
LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation
This is doable in v1 as well as v2. However, since the code snippet was made in v0, here's the updates you'll need to do:
Reset the form to new values after submission#
To make sure that the async default values still work (like if the server returns something unexpected), reset the form within
onSubmitafter you invalidated the query.const query = useQuery({ queryKey: ["data"], queryFn: () => Promise.resolve({ firstName: "John", }), }); const form = useForm({ defaultValues: query.data ?? { firstName: "" }, onSubmit: async ({ value }) => { console.log("submitted"); // Request the updated value from the backend query.refetch(); // Optimistically reset the form to the new values. If the query happens to return a different // value, it will allow it due to being reset form.reset(value); }, });Keeping the button reactive#
You'll need to subscribe to the state you want, in this case
form.state.isDefaultValue. Otherwise, it will not behave reactively. Since we reset the form to the new values optimistically,isDefaultValuewill start off astrueagain, so there's no need to lsiten to submission state.<form.Subscribe selector={(state) => state.isDefaultValue}> {(isDefaultValue) => ( <button disabled={isDefaultValue}>Submit</button> )} </form.Subscribe>https://github.com/user-attachments/assets/f7fcaaaf-387f-477e-b35b-be66f9306bb0
LeCarbonator(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Unfortunately not, since it breaks the rules of React and therefore will cause malfunctions with the React compiler.