【问题标题】:My Next.JS-Redux app compiles 2 page at once我的 Next.JS-Redux 应用程序一次编译 2 页
【发布时间】:2022-01-30 02:20:38
【问题描述】:

在使用 window.location.href 重定向后,我在终端上得到了这些编译

wait  - compiling /login/verify (client and server)...
wait  - compiling /login (client and server)...

它应该去 /login/verify,但因为它而返回 /login。

这是我的重定向代码:

componentWillReceiveProps(nextProps) {
  if(nextProps.authCode == '0')
  {
    // redirect to homepage
    setTimeout(() => {
      window.location.href = '/'
    }, 2000)
  } else if(nextProps.authCode == '20') {
    setTimeout(() => {
      window.location.href = '/register/verify'
    }, 2000)
  } else if(nextProps.authCode == '30')
  {
    setTimeout(() => {
      window.location.href = '/login/verify'
    }, 2000)
  } else {
    this.setState({
      alertLoading: false,
      alertFailed: true,
      alertMsg: nextProps.authInfo
    })
  }
}

【问题讨论】:

    标签: reactjs redux react-redux next.js redux-persist


    【解决方案1】:

    尝试使用useEffectcomponentDidMount 进行重定向。

    您也可以使用next/router 中的routerrouter.replace('/login/verify') 方法进行重定向。

    我建议检查一下https://nextjs.org/docs/api-reference/next/router#usage 更多重定向示例

    【讨论】:

    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多