why use useTransition vs useActionState for Server Actions
Answered
Jumbo flying squid posted this in #help-forum
Jumbo flying squidOP
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?
Answered by luis_llanes
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
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
2 Replies
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
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
Answer