【问题标题】:React router not working on live production反应路由器不适用于现场生产
【发布时间】:2021-01-21 12:37:35
【问题描述】:

我已经使用 create-react-app 构建了一个 UI amazon-clone 它只显示虚拟数据。 问题是在将其发布到 Vercel 后,路由无法按预期工作!单击链接后,您会看到一个空白页面“URL 参数正确”,您必须手动重新加载页面才能工作! 如果你点击了一个按钮,没有事件触发,你会得到一个空白页面!

我将所有路由都封装到 MainRoute 组件:

const MainRoute = withRouter(({ location }) => {
return (
<>
  {location.pathname !== "/login" && location.pathname !== "/signup" ? (
    <Header />
  ) : null}
  <Switch>
    <Route exact path="/" render={() => <Home />} />
    <Route exact path="/products" render={() => <Products />} />
    <Route
      exact
      path="/products/:productID"
      render={() => <ProductPage />}
    />
    <Route path="/checkout" render={() => <Checkout />} />
    <Route path="/payment" render={() => <Payment />} />
    <Route path="/login" render={() => <Login />} />
    <Route path="/signup" render={() => <Signup />} />

    <Route render={() => <NotFoundPage />} />
  </Switch>
  {location.pathname !== "/login" && location.pathname !== "/signup" ? (
    <Footer />
   ) : null}
 </>
 );
});

 export default withRouter(MainRoute);

我的应用组件:

function App() {

return (
 <div className="app_wrapper">
  <Router>
    <MainRoute />
  </Router>
  </div>
 );
 }

export default App;

回购 https://github.com/aseelban/amazon-clone-app 关联: https://amazon-clone-app-llyl1tfcn.vercel.app/

【问题讨论】:

  • /checkout 路由是否在您的本地计算机上完美运行?这个问题只发生在生产环境中吗?
  • 不幸的是只有 /login 和 /singup 工作!其余的没有。是的,在 localhost 上一切正常
  • 你不需要使用withRouter,你可以使用useHistory钩子来访问history对象。我认为这可能是个问题。
  • 检查reactrouter.com/web/api/Hooks/usehistory 以了解如何以及在何处使用useHistory。仅在需要访问history 对象时使用useHistory,不需要在不需要访问history 对象的那些组件(功能性)中使用它。

标签: javascript reactjs react-router material-ui vercel


【解决方案1】:

它可以正常工作(under Brave browser) 身份验证路由,您能否指定出现问题的路由。!

【讨论】:

  • 尝试使用谷歌浏览器,它不适用于除登录和注册之外的所有路由。
  • 去检查你的生产版本中的控制台输出错误
  • 它显示长度未定义错误:react-dom.production.min.js:152 Uncaught TypeError: Cannot read property 'length' of undefined at Ko (react-dom.production.min.js:152 ) 在 ia (react-dom.production.min.js:160) 在 Object.aa [as useEffect] (react-dom.production.min.js:160) 在 Object.t.useEffect (react.production.min. js:24) 在 dn (makeStyles.js:191) 在 p (makeStyles.js:228) 在 withStyles.js:55 在 Xo (react-dom.production.min.js:153) 在 Na (react-dom. production.min.js:173) 在 vl (react-dom.production.min.js:265)
  • 很难调试 minify 生产版本。
  • 你可以使用开发 react 版本来部署
【解决方案2】:

谢谢大家的帮助。 我通过删除 HOC withStyles 并改用 react-jss 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 2017-01-16
    • 2021-02-15
    • 2019-10-03
    • 2021-04-03
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2020-02-19
    相关资源
    最近更新 更多