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
extended-yellowOP•16mo 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
conscious-sapphire•16mo ago
No, you do not.
Try adding your
{success: true}
in your server action, then add a useActionState
to get the configextended-yellowOP•16mo 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?
conscious-sapphire•16mo ago
useFormState got renamed to useActionState
I need to update the docs and a blog post I wrote
extended-yellowOP•16mo ago
I have an error with useActionState, but I think I'm on the latest @next...
conscious-sapphire•16mo ago
Huh, weird. Try the React 19 RC?
extended-yellowOP•16mo 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?conscious-sapphire•16mo 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 yeahextended-yellowOP•16mo 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)extended-yellowOP•16mo ago
yeah so this:
has this console output when I submit

extended-yellowOP•16mo 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 🙂
extended-yellowOP•16mo 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.
extended-yellowOP•16mo ago
code:
conscious-sapphire•16mo 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
extended-yellowOP•16mo ago
ooooooh okay, I've seen that issue. Thanks for the heads up