I am using pages router and useRouter from next/navigation for refreshing the page when an action is taken by user and a certain condition is met.
When the action is taken, the page refreshes properly. But when i take the same action again, the page wont refresh.
I can confirm that my function is being called cuz it shows a toast before the refresh. So it is router.refresh() that's the only thing bugging out.
i have the following handler in a parent component:
const router = useRouter()
const handleDelete = async (asset: Pick<AssetData, "id" | "name">) => {
const isConfirmed = await deleteModal(`Are you sure you want to delete ${asset.name}?`)
if (!isConfirmed) {
return
}
const response = await deleteAsset(asset.id)
toast({
variant: response.success ? "default" : "destructive",
title: response.message,
})
if (!response.success) {
return
}
router.refresh()
}
which is used inside a child:
<Button
onClick={async (e) => {
e.stopPropagation()
handleDelete(asset)
}}
the function is being passed to the child component through a context:
const { handleDelete } = useContext(MyContext)