MERN应用:渲染时的React'
问题摘要
我正在使用MERN应用程序。
到目前为止,我已经建立了后端,并且只是开始在前端添加路线。
在将路线添加到前端的过程之后,出现了问题。问题是屏幕上没有任何东西出现。在控制台中,以下错误显示:
问题详细信息
目录快照
Problem Summary
I am working on a MERN app.
So far I have built the backend, and just starting adding routes in the frontend.
After the process of adding routes to the frontend, an issue arose. The issue is that nothing shows up on the screen. In the Console, the following error shows:
Problem Details
Directory Snapshot ????
The directory where my app is looks like this:
App.js ????
// import bootstrap
import "bootstrap/dist/css/bootstrap.min.css";
// import Routers
import {BrowserRouter as Router, Route} from "react-router-dom";
import Navbar from "./components/navbar.component";
import ExerciseList from "./components/exercises-list.component";
import EditExercise from "./components/edit-exercise.component";
import CreateExercise from "./components/create-exercise.component";
import CreateUser from "./components/create-user.component";
function App() {
return (
<Router>
<Navbar/>
<br/>
<Route exact path="/" component={ExerciseList}/>
<Route path="/edit/:id" component={EditExercise}/>
<Route path="/create" component={CreateExercise}/>
<Route path="/user" component={CreateUser}/>
</Router>
);
}
export default App;
package.json ????
{
"name": "exercise-tracker",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^5.1.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
navbar.component.js ????
import React, {Component} from 'react'
import { Link } from 'react-router-dom';
export default class Navbar extends Component {
render() {
return (
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<Link to="/" className="navbar-brand">Exercise Tracker</Link>
<div className='collapse navbar-collapse'>
<ul className="navbar-nav mr-auto">
<li className="navbar-item"><Link to="/" className="nav-link">Exercises</Link></li>
<li className="navbar-item"><Link to="/create" className="nav-link">Create Exercise Log</Link></li>
<li className="navbar-item"><Link to="/user" className="nav-link">Create User</Link></li>
</ul>
</div>
</nav>
)
}
}
What I have tried
I tried following the steps on the console and wrapping in a in App.js (I also update App.js's line import {BrowserRouter as Router, Route} from "react-router-dom"
to import {BrowserRouter as Router, Route, Routes} from "react-router-dom"
).
This solves the problem of the white page and the console errors. However, I do not see anything in the body (which is what I expect upon clicking the links in the navbar) and I see the following errors in the console:
At this point I did not know where to go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
中,React-Router-dom@6
route
组件只能由ROUTES
组件或其他ROUTE> ROUTE> ROUTE
渲染。成分。路由
组件API也与V6发生了显着更改,它们在单个元素
prop prop areact> react node
。路由
组件包装在路由
组件中。路由
组件'element
prop作为JSX示例:
请参阅从v5 指南迁移,以进一步破坏V6的变化。
In
react-router-dom@6
theRoute
components can only be rendered by theRoutes
component or otherRoute
components. TheRoute
component API also changed significantly from v6, they render their content on a singleelement
prop taking aReactNode
, a.k.a. JSX, value.Route
components in aRoutes
component.Route
components'element
prop as JSXExample:
See the Migrating from v5 guide for further breaking changes in v6.