Routing after Delete item
Unanswered
Little Gull posted this in #help-forum
Little GullOP
I have a situation where I can navigate to different paths to delete a post:
1. /home -> /posts -> /posts/{id} – nested page, should redirect back to /posts after delete
2. /home -> /profile -> /posts/{id} – nested page, should redirect back to /profile after delete
3. /home -> /posts – delete directly from list, no redirect needed
Currently, I pass a callbackUrl prop and keep it in the URL to determine where to redirect after the delete server action. This means I always have to carry the callbackUrl through links and forms.
Is this considered good practice? How do people usually handle redirects like this in Next.js?
After working with React Router and history, this approach feels a bit confusing.
Also, if I want a different redirect after updating a post, should I add another prop like redirectTo? That feels messy…
1. /home -> /posts -> /posts/{id} – nested page, should redirect back to /posts after delete
2. /home -> /profile -> /posts/{id} – nested page, should redirect back to /profile after delete
3. /home -> /posts – delete directly from list, no redirect needed
Currently, I pass a callbackUrl prop and keep it in the URL to determine where to redirect after the delete server action. This means I always have to carry the callbackUrl through links and forms.
Is this considered good practice? How do people usually handle redirects like this in Next.js?
After working with React Router and history, this approach feels a bit confusing.
Also, if I want a different redirect after updating a post, should I add another prop like redirectTo? That feels messy…
3 Replies
Giant panda
Hi there
Yes this is the best practice as the component gets the redirect url to redirect to or none to stay in the same route. But I should ask you whether you are using a function to call to redirect or a string that has the route to redirect to?
You should see if the route change is in the parent of the child as it makes the parent or the child client side component
Yes this is the best practice as the component gets the redirect url to redirect to or none to stay in the same route. But I should ask you whether you are using a function to call to redirect or a string that has the route to redirect to?
You should see if the route change is in the parent of the child as it makes the parent or the child client side component
when you are in the /home/profile and delete post you no need redirect nextjs have refresh and revalite you can use those
onClick={async () => {
await deletePost(id)
router.back()
}}
await deletePost(id)
router.back()
}}