React › Server Actions & Data Mutations
How would you refresh data shown on a page after a server action successfully changes something in the database?
IntermediateAnswer
Frameworks that support server actions, like Next.js, provide a function such as revalidatePath, which tells the framework that data for a specific page is now outdated and should be fetched fresh the next time it is requested, ensuring users see the updated information right away.
async function deletePost(id) {
'use server';
await db.posts.delete(id);
revalidatePath('/posts');
}Real-world example A blog admin panel automatically shows an updated post list immediately after deleting a post, since the server action tells the framework to refresh the posts page's data right after the deletion completes.