#why use useTransition vs useActionState for Server Actions

1 messages · Page 1 of 1 (latest)

muted tinsel
#

I attached an example from NextJS Server Actions docs and another from Reacts Server Actions docs. Both seem to give you access to isPending, errors, and setting state based on the result. Why would you use one vs the other?

sweet belfryBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

serene jay
#

Basically what an action is: an async function that runs inside a transition.

Basically those two examples are equivalent in the sense that both are an async function (we know for a fact that they are since they’re server actions, and for that they must be) and both are running inside a transition:

the first example is imperative and the second is declarative since React is taking care of that for you, internally wrapping the Sever Action call inside a transition when you call it from useActionState and inside the action prop of the form, that’s why you get access to the isLoading while the task is running inside the transition in the background.

The “useTransition” of the first example is just there to get access to the loading state while the task is running

sweet belfryBOT