How can I show success on client using server action?
I am trying to useForm method. I want to show some nice UI when the form submits correctly. I am using NextJS and server actions. In my useForm config, I am trying to do this:
But the state is old, so I think I'm looking at the wrong object? My server action for example would return {success: true/false}
15 Replies
deep-jadeOP•2y ago
omg, do I just have to have a useEffect with state as a dependency?
seems to work fine, but would be nice if there was a way I can do it in the config of useForm
like-gold•2y ago
No, you do not.
Try adding your
{success: true} in your server action, then add a useActionState to get the configdeep-jadeOP•2y ago
@crutchcorn hmmm I thought I tried that, but ill check it out again. Thanks
is it useFormState or useActionState?
Can you show me an example of how you would implement that in the onSubmit function?
like-gold•2y ago
useFormState got renamed to useActionState
I need to update the docs and a blog post I wrote
deep-jadeOP•2y ago
I have an error with useActionState, but I think I'm on the latest @next...
like-gold•2y ago
Huh, weird. Try the React 19 RC?
deep-jadeOP•2y ago
regardless, if I figure that out, is it just as simple as doing this?
I just look at the
state var from the hook?like-gold•2y ago
You might wanna add an
if in the mergeForm
To conditionally handle the merge from the server in case it returns non form data
But otherwise yeahdeep-jadeOP•2y ago
I think I have to review the mergeForm stuff, I mostly just ripped that from the example
Is TSForm still a rather new product from TS?
Also, I think useActionState is only available in the newest react, so I'll probably still try and useFormState. Do you think the same thing would fix there? I originally had that, but the
state in the onSubmit was One behind (the first submit it would have initial data, second submit would have the response from the server action)deep-jadeOP•2y ago
yeah so this:
has this console output when I submit

deep-jadeOP•2y ago
so the onSubmit is still old, but the transformState is correct, is the merge incorrect?
@crutchcorn hate to ping direct, but would love to put a pin in this! I would be open to making relevnat suggestions to documentation 🙂
deep-jadeOP•2y ago
@crutchcorn to make my example a bit more verbose, I am using the same code from the TanStack Form documentation, with the server validation of age. Let me know how I can change the code to have an updated state, without needing a useEffect.
deep-jadeOP•2y ago
code:
like-gold•2y ago
@Tyler hold off on our SSR usage. It leaks server code to the client, which was news to me.
We'll need to revisit the API some other time
deep-jadeOP•2y ago
ooooooh okay, I've seen that issue. Thanks for the heads up