Server Action and weird behavior with useTransition
Unanswered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
I'm using an async useTransition hook with a server action inside. I'm getting a weird result. Basically the promise is running two times (values are then duplicated in DB because of that) and even when the promise is finished (success or error) the useTransition pending state keeps loading indefinitely.
useTransition might not even be the right way to achieve that ?
In client:
Server action:
useTransition might not even be the right way to achieve that ?
In client:
startTransition(async () => {
const res = await action(formData) <== running two times, why ?
if (res?.success) {
toast.error("error")
return
}
})
Server action:
"use server"
...
export async function action(formData) {
const options: AxiosRequestConfig = {
method: 'POST',
url: ...,
headers: {
...
},
data: formData,
}
try {
const res = await axios<{ id: string }>(options)
await otherAction({
...
})
return redirect(ROUTE_PATH)
} catch (e) {
return { success: false }
}
}
23 Replies
you could post more info here, for now I think it's possible to not use the transition
I suspect they are mainly useful for making the action cancelable, and the synchronous actions that might be associated with it better integrated with client-side React
So there is no definitive answer to this but for the moment you can dump the transition until it's clearer what it actually does
seeing something twice might be related to React strict mode that fires effect twice, or just be a plain bug
Saltwater CrocodileOP
@Eric Burel Thanks! I'll try without the useTransition and use simple useState to show state updates. For the server action fired twice, I tried to disable reactStrictMode in next config but still not working as expected.
useFormStatus works well though
instead of maintaining your own state
but maybe it needs the transition to work in the first place
you should test that first
Saltwater CrocodileOP
Does useFormStatus works well with React Hook Form ?
@Saltwater Crocodile Does useFormStatus works well with React Hook Form ?
it's unrelated I think
it's about following the state of the server action
rhf is about managing the client-side input state
Saltwater CrocodileOP
Yes, but I was wondering if useFormStatus was working only for simple form with action={} prop or it could work in other use case
Saltwater CrocodileOP
@Eric Burel I have more information about the "duplicated value" when I'm using server action. It's like the server action is timing out, and then restarting when it's been upload a file for too long for instance
@Saltwater Crocodile Yes, but I was wondering if useFormStatus was working only for simple form with action={} prop or it could work in other use case
I suspect that's exactly why the transition is needed
but not confirmed and not tested
basically transitions, suspense, are a link between "asynchronous world" and "React world" which doesn't really allow asynchronous calls client-side
(in my understanding which is highly limited right now, this is very new)
@Saltwater Crocodile <@769111741098622976> I have more information about the "duplicated value" when I'm using server action. It's like the server action is timing out, and then restarting when it's been upload a file for too long for instance
it happens locally too or only eg on Vercel which have a very short timeout?
interesting anyway
Saltwater CrocodileOP
I have this behavior in local dev but also in prod Vercel. But I guess for now I will go back to Tanstack Query / SWR for mutations