简体   繁体   English

从 React/Next.js 中的 getInitalProps 重定向

[英]Redirecting from getInitalProps in React/Next.js

I am using React and Next.js and trying to redirect a user from a page when the data for that page is not available using Router.push('/another-page') .我正在使用 React 和 Next.js 并尝试使用Router.push('/another-page')当该页面的数据不可用时从页面重定向用户。

To do this I am checking for a status code in getInitalProps and applying a conditional.为此,我正在检查getInitalProps的状态代码并应用条件。 It looks like this:它看起来像这样:

  const statusCode = action.currentArticle ? 200 : 404

  if (isServer) res.statusCode = statusCode

  if (statusCode === 404) {
    Router.push('/')
  }

The status code is being set properly and it makes it inside the conditional, at which point I am greeted with this error: No router instance found. You should only use "next/router" inside the client side of your app.状态代码设置正确,并使其在条件内,此时我收到此错误:未No router instance found. You should only use "next/router" inside the client side of your app. No router instance found. You should only use "next/router" inside the client side of your app.

Actually, I am getting the same error no matter WHERE in the component's lifecycle events I try to redirect, and am getting little info online about this error.实际上,无论我尝试重定向的组件生命周期事件中的任何位置,我都会遇到相同的错误,并且在网上获得的有关此错误的信息很少。

The pattern of redirecting from getInitalProps can be seen in this next.js wiki: HEREgetInitalProps重定向的模式可以在 next.js wiki 中看到: 这里

Any ideas on why this error is occurring or how to fix it are much appreciated ;)非常感谢有关为什么会发生此错误或如何解决此错误的任何想法;)

I am using React and Next.js and trying to redirect a user from a page when the data for that page is not available using Router.push('/another-page') .我正在使用React和Next.js并尝试使用Router.push('/another-page')不能从该页面重定向用户时使用该页面的数据。

To do this I am checking for a status code in getInitalProps and applying a conditional.为此,我要检查getInitalProps的状态代码并应用条件代码。 It looks like this:看起来像这样:

  const statusCode = action.currentArticle ? 200 : 404

  if (isServer) res.statusCode = statusCode

  if (statusCode === 404) {
    Router.push('/')
  }

The status code is being set properly and it makes it inside the conditional, at which point I am greeted with this error: No router instance found. You should only use "next/router" inside the client side of your app.状态代码已正确设置,并将其置于条件代码之内,这时我将收到以下错误消息:未No router instance found. You should only use "next/router" inside the client side of your app. No router instance found. You should only use "next/router" inside the client side of your app.

Actually, I am getting the same error no matter WHERE in the component's lifecycle events I try to redirect, and am getting little info online about this error.实际上,无论我尝试重定向的组件的生命周期事件在哪里,我都会遇到相同的错误,并且几乎没有关于此错误的在线信息。

The pattern of redirecting from getInitalProps can be seen in this next.js wiki: HEREgetInitalProps重定向的模式可以在以下next.js Wiki中看到: HERE

Any ideas on why this error is occurring or how to fix it are much appreciated ;)对此错误发生原因或解决方法的任何想法都值得赞赏;)

I am using React and Next.js and trying to redirect a user from a page when the data for that page is not available using Router.push('/another-page') .我正在使用React和Next.js并尝试使用Router.push('/another-page')不能从该页面重定向用户时使用该页面的数据。

To do this I am checking for a status code in getInitalProps and applying a conditional.为此,我要检查getInitalProps的状态代码并应用条件代码。 It looks like this:看起来像这样:

  const statusCode = action.currentArticle ? 200 : 404

  if (isServer) res.statusCode = statusCode

  if (statusCode === 404) {
    Router.push('/')
  }

The status code is being set properly and it makes it inside the conditional, at which point I am greeted with this error: No router instance found. You should only use "next/router" inside the client side of your app.状态代码已正确设置,并将其置于条件代码之内,这时我将收到以下错误消息:未No router instance found. You should only use "next/router" inside the client side of your app. No router instance found. You should only use "next/router" inside the client side of your app.

Actually, I am getting the same error no matter WHERE in the component's lifecycle events I try to redirect, and am getting little info online about this error.实际上,无论我尝试重定向的组件的生命周期事件在哪里,我都会遇到相同的错误,并且几乎没有关于此错误的在线信息。

The pattern of redirecting from getInitalProps can be seen in this next.js wiki: HEREgetInitalProps重定向的模式可以在以下next.js Wiki中看到: HERE

Any ideas on why this error is occurring or how to fix it are much appreciated ;)对此错误发生原因或解决方法的任何想法都值得赞赏;)

I am using React and Next.js and trying to redirect a user from a page when the data for that page is not available using Router.push('/another-page') .我正在使用React和Next.js并尝试使用Router.push('/another-page')不能从该页面重定向用户时使用该页面的数据。

To do this I am checking for a status code in getInitalProps and applying a conditional.为此,我要检查getInitalProps的状态代码并应用条件代码。 It looks like this:看起来像这样:

  const statusCode = action.currentArticle ? 200 : 404

  if (isServer) res.statusCode = statusCode

  if (statusCode === 404) {
    Router.push('/')
  }

The status code is being set properly and it makes it inside the conditional, at which point I am greeted with this error: No router instance found. You should only use "next/router" inside the client side of your app.状态代码已正确设置,并将其置于条件代码之内,这时我将收到以下错误消息:未No router instance found. You should only use "next/router" inside the client side of your app. No router instance found. You should only use "next/router" inside the client side of your app.

Actually, I am getting the same error no matter WHERE in the component's lifecycle events I try to redirect, and am getting little info online about this error.实际上,无论我尝试重定向的组件的生命周期事件在哪里,我都会遇到相同的错误,并且几乎没有关于此错误的在线信息。

The pattern of redirecting from getInitalProps can be seen in this next.js wiki: HEREgetInitalProps重定向的模式可以在以下next.js Wiki中看到: HERE

Any ideas on why this error is occurring or how to fix it are much appreciated ;)对此错误发生原因或解决方法的任何想法都值得赞赏;)

I am using React and Next.js and trying to redirect a user from a page when the data for that page is not available using Router.push('/another-page') .我正在使用React和Next.js并尝试使用Router.push('/another-page')不能从该页面重定向用户时使用该页面的数据。

To do this I am checking for a status code in getInitalProps and applying a conditional.为此,我要检查getInitalProps的状态代码并应用条件代码。 It looks like this:看起来像这样:

  const statusCode = action.currentArticle ? 200 : 404

  if (isServer) res.statusCode = statusCode

  if (statusCode === 404) {
    Router.push('/')
  }

The status code is being set properly and it makes it inside the conditional, at which point I am greeted with this error: No router instance found. You should only use "next/router" inside the client side of your app.状态代码已正确设置,并将其置于条件代码之内,这时我将收到以下错误消息:未No router instance found. You should only use "next/router" inside the client side of your app. No router instance found. You should only use "next/router" inside the client side of your app.

Actually, I am getting the same error no matter WHERE in the component's lifecycle events I try to redirect, and am getting little info online about this error.实际上,无论我尝试重定向的组件的生命周期事件在哪里,我都会遇到相同的错误,并且几乎没有关于此错误的在线信息。

The pattern of redirecting from getInitalProps can be seen in this next.js wiki: HEREgetInitalProps重定向的模式可以在以下next.js Wiki中看到: HERE

Any ideas on why this error is occurring or how to fix it are much appreciated ;)对此错误发生原因或解决方法的任何想法都值得赞赏;)

To make sure the page never render, we need to add await new Promise(() => {}) to end.为了确保页面永远不会渲染,我们需要添加await new Promise(() => {})来结束。 The promise no needed resolve anything.承诺不需要解决任何问题。

Home.getInitialProps = async ({res}) => {
    if(res) {
      res.writeHead(302, {location: '/dashboard'});
      res.end();
    } else {
      // window.location.href = '/dashboard';
      // Or with SPA redirect
      Router.push('/dashboard');
    }
 
    await new Promise(() => {});

    return {}
}

I am using React and Next.js and trying to redirect a user from a page when the data for that page is not available using Router.push('/another-page') .我正在使用React和Next.js并尝试使用Router.push('/another-page')不能从该页面重定向用户时使用该页面的数据。

To do this I am checking for a status code in getInitalProps and applying a conditional.为此,我要检查getInitalProps的状态代码并应用条件代码。 It looks like this:看起来像这样:

  const statusCode = action.currentArticle ? 200 : 404

  if (isServer) res.statusCode = statusCode

  if (statusCode === 404) {
    Router.push('/')
  }

The status code is being set properly and it makes it inside the conditional, at which point I am greeted with this error: No router instance found. You should only use "next/router" inside the client side of your app.状态代码已正确设置,并将其置于条件代码之内,这时我将收到以下错误消息:未No router instance found. You should only use "next/router" inside the client side of your app. No router instance found. You should only use "next/router" inside the client side of your app.

Actually, I am getting the same error no matter WHERE in the component's lifecycle events I try to redirect, and am getting little info online about this error.实际上,无论我尝试重定向的组件的生命周期事件在哪里,我都会遇到相同的错误,并且几乎没有关于此错误的在线信息。

The pattern of redirecting from getInitalProps can be seen in this next.js wiki: HEREgetInitalProps重定向的模式可以在以下next.js Wiki中看到: HERE

Any ideas on why this error is occurring or how to fix it are much appreciated ;)对此错误发生原因或解决方法的任何想法都值得赞赏;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM