简体   繁体   English

使用 react-router 时 React 应用程序崩溃

[英]React app crashing when react-router is used

I have a react app that works great on localhost.我有一个在本地主机上运行良好的 React 应用程序。 after deploying, I upload the build folder content and the app crashes.部署后,我上传了构建文件夹内容,应用程序崩溃了。 this is the crush screenshot https://www.screencast.com/t/hjkoBmaY9 just a white screen.这是美眉截图https://www.screencast.com/t/hjkoBmaY9只是一个白屏。 I have searched a lot and found that it is caused by the react-router-dom查了很多,发现是react-router-dom引起的

this is my App.js component:这是我的App.js组件:

import React, { Component } from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import Calc from './components/Calc';
import Login from './components/auth/Login';
import MechinaSetup from './components/adminarea/MechinaSetup';
import './App.css';
class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <Route exact path="/" component={Calc}  />
        <Route path="/login" component={Login} />
        <Route path="/mechinasetup" component={MechinaSetup} />
      </BrowserRouter>
    );
  }
}

export default App;

if I remove the BrowserRouter block and run the build again, I see the app,如果我删除BrowserRouter块并再次运行构建,我会看到应用程序,

this is how I see an output这就是我看到输出的方式

import React, { Component } from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import Calc from './components/Calc';
import Login from './components/auth/Login';
import MechinaSetup from './components/adminarea/MechinaSetup';
import './App.css';
class App extends Component {
  render() {
    return (
      // <BrowserRouter>
      //   <Route exact path="/" component={Calc}  />
      //   <Route path="/login" component={Login} />
      //   <Route path="/mechinasetup" component={MechinaSetup} />
      // </BrowserRouter>
      <Calc />
    );
  }
}

export default App;导出默认应用程序;

What can be the reason to that?那可能是什么原因?

here is the git repo of the project.这是该项目的 git repo。 https://github.com/davsev/calc https://github.com/davsev/calc

any help would be amazing.任何帮助都会很棒。

I experienced the same problem.我遇到了同样的问题。

Quick Troubleshoot -快速故障排除 -

  1. Make sure that the components in your Route definitions do not have a duplicate of the Switch Route definition or any for that matter.确保您的路由定义中的组件没有 Switch Route 定义的副本或任何与此相关的副本。
  2. Keep all your Route definition in the App js file将所有路由定义保存在 App js 文件中
  3. Wrap the with the BrowserRouter in the index.js file.在 index.js 文件中使用 BrowserRouter 包装 。 Take it out of the App.js从 App.js 中取出

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

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