router.refresh after creating a post
Answered
Plott Hound posted this in #help-forum
Plott HoundOP
say i have a RSC page /posts that lists posts with prisma . then in a sub client page /posts/[id] i create a new post and save it via my route handler successfully. after saving i use router.push to go back to /posts. but i cant see the new post without doing a browser refresh. is this expected behaviour? do i also need to use router.refresh? thanks
Answered by joulev
just like
or
router.refresh();
router.push();or
router.push();
router.refresh();19 Replies
@Plott Hound say i have a RSC page /posts that lists posts with prisma . then in a sub client page /posts/[id] i create a new post and save it via my route handler successfully. after saving i use router.push to go back to /posts. but i cant see the new post without doing a browser refresh. is this expected behaviour? do i also need to use router.refresh? thanks
yes.
the intended way is to not use a route handler but a server action instead, there you simply call revalidateTag/revalidatePath then you'll see the new post. but in the meantime while server actions are not stable and if you don't want to use unstable software, you have to use router.refresh()
the intended way is to not use a route handler but a server action instead, there you simply call revalidateTag/revalidatePath then you'll see the new post. but in the meantime while server actions are not stable and if you don't want to use unstable software, you have to use router.refresh()
Siamese Crocodile
One way to achieve this is to use the useEffect hook in React (assuming you're using React) on the /posts page. In the useEffect function, you can place the data fetching logic and dependency array to specify that the effect should run whenever the route changes.
@Siamese Crocodile One way to achieve this is to use the useEffect hook in React (assuming you're using React) on the /posts page. In the useEffect function, you can place the data fetching logic and dependency array to specify that the effect should run whenever the route changes.
Plott HoundOP
thanks since the parent /posts is an rsc i was trying to avoid useEffect in this file
Siamese Crocodile
oh...ok
sorry
@joulev yes.
the intended way is to not use a route handler but a server action instead, there you simply call revalidateTag/revalidatePath then you'll see the new post. but in the meantime while server actions are not stable and if you don't want to use unstable software, you have to use router.refresh()
Plott HoundOP
thanks. how could i modify this to also do a router.refresh?
if (data.success) {
toast.success('Routine saved successfully!');
router.push('/routines');
} else {
console.error("Server responded with error:", data.error);
toast.error('Error saving routine.');
}@Plott Hound thanks. how could i modify this to also do a router.refresh?
if (data.success) {
toast.success('Routine saved successfully!');
router.push('/routines');
} else {
console.error("Server responded with error:", data.error);
toast.error('Error saving routine.');
}
add router.refresh() before or after router.push()
Plott HoundOP
thanks i'll try that now
i think both before and after work
Siamese Crocodile
if (data.success) {
toast.success('Routine saved successfully!');
router.push('/routines').then(() => {
router.reload();
});
} else {
console.error("Server responded with error:", data.error);
toast.error('Error saving routine.');
}hope works
also no need of .then
just like
or
router.refresh();
router.push();or
router.push();
router.refresh();Answer
Siamese Crocodile
a ok
Plott HoundOP
router.push('/routines');
router.refresh();worked for me thanks.
nice
Siamese Crocodile
gg
Plott HoundOP
second time you helped me thanks so much @joulev and @Siamese Crocodile you guys are awesome