-
Notifications
You must be signed in to change notification settings - Fork 27.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to redirect? #4931
Comments
The wiki has an entry about this subject. For a more real world example, please see with-apollo-auth example. From what I've seen you should post any future questions not related to actual issues to Next's Spectrum Chat instead. Cheers 🎉 |
@klujanrosas Your example doesnt work in my case |
How redirecting can be so hard with nextjs >:( |
Check the code of this module https://github.com/matthewmueller/next-redirect/ it's actually quite simple, you should redirect in getInitialProps to avoid rendering and fetching unnecessary data. You could also replace https://github.com/matthewmueller/next-redirect/blob/master/index.js#L16 with a |
Maybe some details would be great, instead of just downvoting. Because the example works completely fine for me. |
I believe this is a difference in getInitialProps signature for |
@sergiodxa @jgillich I am doing something very similar to the implementation in |
@klujanrosas how do you redirect with a dynamic route e.g. |
I am not sure why this got closed when it clearly does not answer the question for so many people. There HAS to be a way to do redirects outside |
Hey, all! Issuing a side effect during rendering is not a standard React practice. You should avoid redirecting client-side within Redirects are a side effect, and should occur as so within your component's events: componentDidMount() { // and/or componentDidUpdate, depending on what you're trying to do
if (!auth) {
Router.push('...')
}
}
render() {
if (!auth) {
return null;
}
// ...
} For a functional component: function MyPage() {
useEffect(() => {
if (!auth) {
Router.push('...')
}
})
// ...
} Please open a new thread on our Spectrum if you have any questions or need guidance. |
I try in _app
render(){
if(!auth) Router.push('login')
}
and got error
Error: No router instance found.
You should only use "next/router" inside the client side of your app.
how do redirect do right?
The text was updated successfully, but these errors were encountered: