Next.js Discord

Discord Forum

Success message after server actions

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
hey, I am a beginner developer trying out the server actions for the first time.
I am showing a loading state with the useTransition hook with server actions.
I recommend seeing the code before reading further.
Now, what if I want to show a success toast message after my device has been added? I've watched multiple videos about using server actions in forms like this, including the video of Lee Robinson showing the "next-forms-app" example, but all of them are revalidating the data after the data is added to the database. There is no success message.

https://www.youtube.com/watch?v=dDpZfOQBMaU

1 Reply

American Crow
You can not use toastwithin a server action since it requires state and therefore only works on the client.
You have to return an object from your server action e.g. { message: 'success' }. Which you can than access on the client side with useFormState