Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Next.js sending a response object/body with revalidating path from Server Action

$
0
0

Is it possible to send a response body/object when also revalidating a path inside Server Action in Next.js?

This is my Server Action function:

export async function addTodo(prevState: any, formData: FormData) {    const errors: {        title?: string;        description?: string;    } = {};    const title = formData.get('title')?.toString();    const description = formData.get('description')?.toString();    // Validate title and description    if(errors.title || errors.description) {        return { errors };    }    await dbConnect();    const todo = new Todo({        title,        description    });    await todo.save();    revalidatePath('/');    return { todo: { title, description } };}

Sometimes my formState object which is returned by useFormState hook has todo property after processing the request successfully and sometimes it doesn't. So it doesn't seem to be working reliably.


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>