Next.js Discord

Discord Forum

Next.js Server Actions

Unanswered
Arboreal ant posted this in #help-forum
Open in Discord
Arboreal antOP
When i send post request on an API i get response of whether my task was successful or not.
How do i do the same in server actions. How can i send response to update my ui accordingly?

Consider the example in image. How do i change the ui based on success or failure of saveToDb

11 Replies

@Arboreal ant When i send post request on an API i get response of whether my task was successful or not. How do i do the same in server actions. How can i send response to update my ui accordingly? Consider the example in image. How do i change the ui based on success or failure of saveToDb
The response of a server action is purely based on the decision of the developer.
There is no more Response object in the server action, hence

The error handling depends on how you want to implement it.
You can return boolean and check the result client-side
or you can return data | string where data is your data object and string is your error code
I personally use react-hook-form to handle server errors as well as client-side navigation in one component. But i am yet to be sure how you would do it with pure React
Asiatic Lion
you getting a cookie and do.something else
@Arboreal ant Like where is the value returned
Its where you call the server action :/
const data = serverAction()
then console.log(data) would be true if you return true
Arboreal antOP
but its getting called inside form action
see thats why idk where the return went if its inside the action attribute.

You could use onClick and create a custom handler for it with react-hook-form
Arboreal antOP
Yah thats the problem well thanks for the help ill try out the react hook form