Next.js Discord

Discord Forum

Server Component that lists a db table - when i delete an entry how can i refresh the list?

Answered
Plott Hound posted this in #help-forum
Open in Discord
Avatar
Plott HoundOP
Hi. I have a simple page that lists a db table:
async function fetchRoutines(userId) {
    return await prisma.workoutPlan.findMany({
      where: {
        userId: userId,
      },
      select: {
        id: true,
        name: true,
      }
    });
}
export default async function RoutinesPage() {
  const routines = await fetchRoutines(session.user.id);
    return ( bla bla )
}


in a client/child component i'm handling deleting entries with a button. when the delete is successful the item is removed from the db but it isn't shown in the page, I have to refresh the browser manually. how can i handle this gracefully?
Answered by Yi Lon Ma
using router.refresh()
View full answer

7 Replies

Avatar
using router.refresh()
Answer
Avatar
Plott HoundOP
Thanks since the code i shared is the parent and also a server component i was hoping i could avoid this becuase i will have to make it a client component? and then change my fetch to use a route handler instead of a direct fetch
Avatar
in your client component where you're handling delete
once that delete is done
just call router.refresh
Avatar
Plott HoundOP
ok thank you! i'll give that a try now
That works perfectly. thanks for the help @Yi Lon Ma