How to use Tanstack Form in conjunction with Tanstack Query mutation for proper form.state.isSubmitSuccessful? #1839
Unanswered
moebiuscorzer
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.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
I am using Tanstack Form in conjunction with Tanstack Query (and Tanstack Router). I do not manage to get the form
isSubmitSuccessfulto befalsewhile not having an uncaught Promise at the same time, usingmutatevsmutateAsync.TL;DR
Issue:
mutateas the formonSubmitfunction means the form is always considered as submitted successfully (form.state.isSubmitSuccessful) even if the mutate function failed (which is expected considering mutate is always resolved).mutateAsyncas the formonSubmitfunction means the form is properly considered as submitted unsuccessfully when the mutateAsync function throws, but then there is an uncaught promise.It is unclear what should be the idiomatic way to:
onSubmitfunctionform.state.isSubmitSuccessfulshould befalseI can do
form.handleSubmit(/*...*/).catch((_noop) => {})but that looks like a weird way to integrate the two libraries.Question
Mutation
Let us say I have a mutation called
action = useMutation(/*...*/). If I useaction.mutateas the form submission function, I would expect the failed submission to set theform.state.isSubmitsuccessfultofalsebut this is not the case. After all, sinceaction.mutateis always a resolvedPromise, that makes sense.When I use
action.mutateAsync, then the form state is as expected. However, the code is not correct because I then have an "Uncaught promise" showing up in the console.Key question: what is the idiomatic way to ensure the form.isSubmitSuccessful is
falsewhen the action is failed? Should I use theonErrorcallback to update the form state? How so?When I look at the test code here´, it seems that
form.state.isSubmitSuccessful´ should be false when theform.handleSubmit` throws.mutate vs mutateAsync
I do understand that action.mutate does not return a failed promise since it is awaited and the error is caught without side-effect (as explained in TkDodo's blog).
But if I use mutateAsync, there is an uncaught Promise.
Solution
In the below, using:
Will achieve what is expected: form state is correctly "failed" but that looks like a weird way to integrate both libraries together.
Code
Here is my code:
Versions
Tanstack React Form 1.23.5
Tanstack React Query 5.66.5
Tanstack Router 1.114.3
React 19.0.0
vite 6.1.0
Beta Was this translation helpful? Give feedback.
All reactions