Issues with Next.js server actions and TanStack Form
I some issues with TanStack Form and Next.js server actions. I was following this guide: https://tanstack.com/form/latest/docs/framework/react/guides/ssr
First issue:
Docs show how to validate data but what should I do to go further (e.g save valid data to database)?
I tried to use
onSubmit
prop in createServerValidate
but it never fires.
I also thought that maybe I should get form values from FormData
inside server action but it feels bad because I already had access to those values inside onServerValidate
.
Also why result of createServerValidate
is returned to client right away:
I don't understand how should the flow look like.
Second issue:
formState.isSubmitting
is never true. No matter how long server action lives, it is never showing submitting status.
Only If I add onSubmit
to useForm
and run some async code in here then formState.isSubmitting
goes true.
Third issue:
When I hit submit button then it goes to server action even tough client validation failed. Shouldn't it only go to server if client validation succeded?12 Replies
passive-yellow•15mo ago
1. This API may shift in the future to be a throw instead of a
return
because of this. Truthfully, the Next.js integration is a POC for now more than a prod-ready thing (v0 ftw). I'll probably make this change here soon
2. onSubmit
is a required field, even for SSR. If that's not in the docs, that's an issue. (PRs welcomed 🙂 )
3. Maybe, but this is up for debate. Feel free to make a new issue to discuss the pros and consfair-roseOP•15mo ago
Thanks for answer
graceful-blue•14mo ago
@crutchcorn@Vlk Romanov I just encountered the same issues and I think my view matches.
Few things I would recommend a much extended example. It is too basic and unfortunately the documentation is not extensive enough yet.
2. I would have totally expected the isSubmitting to match the server action execution time, and no the onSubmit function, which is empty for me as it does nothing / is not needed really.
extended-salmon•6mo ago
Hey @crutchcorn , I believe the Next.js integration still has issues. Is the integration still a POC, or is it now production-ready (v1)?
I'm noticing problems with the isSubmitting property, and the server action is always called, even when the form is invalid.
metropolitan-bronze•6mo ago
Having the same issue. isSubmitting never changes.
extended-salmon•6mo ago
I believe this related of action and onSubmit. onSubmit is not preventing form action to be triggered, and isSubmitting is not looking to the form action function. If you use useFormStatus (https://react.dev/reference/react-dom/hooks/useFormStatus) the peding on the actions works. But they are not in sync. I hade similar issues with react-hook-form and i need to prevent the submit and trigger a requestSubmit to make it working.
useFormStatus – React
The library for web and native user interfaces
metropolitan-bronze•6mo ago
I don't think it's just NextJS. I see the same isSubmitting issue in Tanstack Start + Form integration if you use the form like this (which is the style shown in the Tanstack Form + Start Docs and their github example):
extended-salmon•6mo ago
The integrations with react 19 seems not be working, and another things:
onServerValidate
is required but does not validate the form using the validators. I believe the same validation the client run (via validators) should run on server too...
after looking for the source code, we can pass a validator to onServerValidate and it will use it to validate the form.
Another issue i've notice is that the useTransform example they have does not work. The error comming from the server is not been merged in the current formgenerous-apricot•6mo ago
I couldn't get the form in nextjs to work with a server action.
the
initialFormData
from tanstack forms'nextjs has undefined
data instead of the defaultValues push to it.
extended-salmon•6mo ago
With this code is possible to use client side validation without triggering the client navigation validation without triggering the action
But i'm not able to update the form state with the action response...
generous-apricot•6mo ago
did you
useTransform
?extended-salmon•6mo ago
Yes, useTransform is not working