Next.js Discord

Discord Forum

Handling post-server actions with useActionState

Answered
Braque Francais posted this in #help-forum
Open in Discord
Avatar
Braque FrancaisOP
The most basic example of this would be showing a toast after a server action completes. This is what I'm currently trying:

const [lastResult, action, isPending] = useActionState(myServerAction, undefined);
const doSomething = async (formData: FormData) => {
        const result = await action(formData); // action doesn't return a promise, so await is doing nothing

        // Do something, like show a toast

        return result;
    }

    return <form action={doSomething}>........


I feel like this should work, yet action does not await. Is there a way to do something like this? It makes me think I'm missing something fundamental about how this should work. Thanks!
Answered by Alfonsus Ardani
This works
Image
View full answer

1 Reply