Skip to content
Source
Sign inSign up
TanStack/form

Form options are stale after submit #1051

Send to my agent
Closedshx-riff(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe 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

  1. Type "Johny" in the input
  2. Click the submit button
  3. 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.

  1. I wonder maybe the formApi.update can be called during the render

    Unfortunately not, since it breaks the rules of React and therefore will cause malfunctions with the React compiler.

  2. crutchcorn(GitHub profile, opens in a new tab) added labelstype: bug

  3. While it's not a fix itself, but a workaround for this specific case, I fixed the issue by calling formApi.reset() in onSubmit, looks legit enough

  4. I 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.reset serves 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 isFieldDirty that 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.

  5. LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation

  6. 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 onSubmit after 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, isDefaultValue will start off as true again, 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

Sign in to join this conversation.