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
Plott HoundOP
Hi. I have a simple page that lists a db table:
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?
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?
7 Replies
using router.refresh()
Answer
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
in your client component where you're handling delete
once that delete is done
just call router.refresh
Plott HoundOP
ok thank you! i'll give that a try now
That works perfectly. thanks for the help @Yi Lon Ma