redirect() isn't working?
Answered
Gecco posted this in #help-forum
GeccoOP
Hi, I added redirect() to my code and it doesn't seem to redirect to the desired endpoint when clicked.
window.location.href = URL seems to work but I'm unsure what I did wrong with the former. I am directing from /docs/api to /docs/api/:id, could such be causing the issue? ThanksAnswered by joulev
redirect() is not server-only but it needs to be caught by a React error boundary for the redirect to work. onClick is not under a React error boundary, so redirect() doesn't work there. use router.push instead8 Replies
What's the code you're using in your redirect
Might want to try
router.pushOr just stick with window.location
@Jesse What's the code you're using in your redirect
GeccoOP
It's within an
onClick event@Gecco It's within an `onClick` event
I would use router.push for that
I think
redirect() is server-onlyredirect() is not server-only but it needs to be caught by a React error boundary for the redirect to work. onClick is not under a React error boundary, so redirect() doesn't work there. use router.push insteadAnswer
GeccoOP
Using
<Link> worked alright, thanks regardless