简体   繁体   English

React Router 渲染空白页面

[英]React Router rendering blank page

I have this issue alot using react router, it just makes my whole app blank.我在使用 react router 时经常遇到这个问题,它只会让我的整个应用程序空白。

the ] at the end was a typo and wouldn't render a blank page, it would just give some normal error最后的 ] 是一个拼写错误,不会呈现空白页面,它只会给出一些正常的错误

This is my App.js:这是我的 App.js:

import './App.css';
import { BrowserRouter as Route, Router, Routes, Link } from 'react-router-dom';
import Home from './components/home';
import About from './components/about'; 
import Projects from './components/projects';

function App() {
  return (
    <Router>
      <header>
        <div className='logo'>MyPortfolio</div>
        <h2>Eshwar Tangirala</h2>
        <ul>
           <Link><li>Home</li></Link> 
           <Link to="/about"><li>About</li></Link>          
           <Link to="/projects"><li>Projects</li></Link>       
        </ul>
      </header>

      <Routes>
        <Route path="/" element={<Home/>}/>
        <Route path="/about" element={<About/>}/>
        <Route path="projects" element={<Projects/>}/>
      </Routes>
    </Router>
  );
}
export default App;

My other components, like home, about, and projects just have an H1 with their name on it.我的其他组件,如 home、about 和 projects,只有一个 H1,上面有它们的名字。

You have mixed up your imports:你混淆了你的进口:

import { BrowserRouter as Route, Router, Routes, Link } from 'react-router-dom';

You've:你有:

  • Imported a BrowserRouter as the Route component导入BrowserRouter作为Route组件
  • Imported the low-level Router which is missing some required props to be passed to it导入了缺少传递给它的一些必需道具的低级Router

Fix the imports:修复导入:

  • Import BrowserRouter as Router导入BrowserRouter作为Router
  • Import the Route component导入Route组件

Correct imports正确导入

import { BrowserRouter as Router, Route, Routes, Link } from 'react-router-dom';

And if not a copy/paste typo, remove the trailing ] on the App export:如果不是复制/粘贴拼写错误,请删除App导出中的尾随]

export default App;] to export default App; export default App;] export default App; . .

编辑 react-router-rendering-blank-page

If the code running in your editor is the same as the above snippet, you have a typo in your export (the extra ] )如果在您的编辑器中运行的代码与上面的代码片段相同,则您的导出中有错字(额外的]

export default App;

Check your default App export remove this ] you have an array ] closing tag.检查您的默认 App export 删除此] you have an array ]结束标记。 Your default export is should be like this:您的默认导出应该是这样的:

export default App;

Not不是

export default App;]

Fix the imports as stated above and then try to wrap your entire app in the browserRouter like this on the index.js, not the app.js如上所述修复导入,然后尝试将整个应用程序包装在 browserRouter 中,就像这样在 index.js 上,而不是在 app.js 上

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { BrowserRouter } from 'react-router-dom';

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById('root')
);

(it might also be an issue with the react-router-dom version, since some of them use Switch instead of Routes , which one are you using? is something logged on the console when you run your app?) (这也可能是 react-router-dom 版本的问题,因为它们中的一些使用Switch而不是Routes ,您使用的是哪一个?运行应用程序时控制台上是否记录了某些内容?)

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

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