React 路由无法读取导航上的路径名属性
我最近开始使用 React 开发前端应用程序,并且拥有一点 React-Native 编程经验。 为了提高我对 React 的了解,我学习了官方文档并学习了 udemy 课程。 因此,我正在构建一个小型测试应用程序,在其中使用反应路由(遵循我们在 udemy 课程中看到的内容)。 但是,我有以下与路由相关的错误(我附上错误屏幕)
我在网上的堆栈上搜索了可能的解决方案,例如降级react-router-dom,或修改历史声明,但不幸的是我仍然收到此错误,并且我无法理解它来自何处。 您有什么建议或建议吗?
我的应用配置:
- package.json
"name": "app1",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.8.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"history": "^5.3.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-redux": "^7.2.8",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.0",
"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"
]
}
}
- paths.js
import React, { Component } from 'react';
import Component1 from './functional/component1';
import Component2 from './functional/component2';
import Component3 from './functional/component3';
//import container home-page
import Container1 from './containers/container1';
import Header from './containers/header';
import history from './utils/history';
//react router import
import { Router, Route } from 'react-router';
class Routes extends Component {
render() {
return (
<div>
<Router history={history.location} navigator={history} >
<div>
<Header />
<Route path="/" component={Container1} />
<Route path="/component1" component={Component1} />
<Route path="/component2" component={Component2} />
<Route path="/component3" component={Component3} />
</div>
</Router>
</div>
)
}
}
export default Routes;
-history.js
var createHistory = require('history').createBrowserHistory;
export default createHistory();
I have recently started developing frontend apps with React and I come from a little programming experience with react-native.
To improve my knowledge on react I studied both the official documentation and took courses on udemy.
So I'm building a small test app in which to use react routing (following what we saw in the udemy course).
However, I have the following error related to routing (I attach the error screen)
I have searched here on the stack that online for possible solutions, such as downgrading react-router-dom, or modifying the history declaration, but unfortunately I still get this error and I can't understand what it comes from.
Do you have any suggestions or advice?
My app configuration:
- package.json
"name": "app1",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.8.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.4",
"@testing-library/user-event": "^13.5.0",
"history": "^5.3.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-redux": "^7.2.8",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.0",
"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"
]
}
}
- routes.js
import React, { Component } from 'react';
import Component1 from './functional/component1';
import Component2 from './functional/component2';
import Component3 from './functional/component3';
//import container home-page
import Container1 from './containers/container1';
import Header from './containers/header';
import history from './utils/history';
//react router import
import { Router, Route } from 'react-router';
class Routes extends Component {
render() {
return (
<div>
<Router history={history.location} navigator={history} >
<div>
<Header />
<Route path="/" component={Container1} />
<Route path="/component1" component={Component1} />
<Route path="/component2" component={Component2} />
<Route path="/component3" component={Component3} />
</div>
</Router>
</div>
)
}
}
export default Routes;
-history.js
var createHistory = require('history').createBrowserHistory;
export default createHistory();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发行
低级
路由器
需要与V5中的几个不同的道具。router
您错误地使用
历史
prop而不是位置
prop,这就是为什么位置
在组件中不确定并且无法阅读PathName
。此外,在
react-rout-dom@6
路由
组件中必须包装在ROUTES
组件中,以使路由路径匹配和渲染工作。解决方案
传递正确的道具,然后将
路由
组件包装在路由
组件中。Issue
The low-level
Router
takes a few different props than it did in v5.Router
Router source
You are incorrectly using a
history
prop instead of thelocation
prop, and this is whylocation
is undefined in the component and unable to read apathname
.Additionally, in
react-router-dom@6
theRoute
components necessarily need to be wrapped in theRoutes
component in order for route path matching and rendering to work.Solution
Pass the correct required props and wrap the
Route
components inRoutes
component.您正在使用
react router v6
,在v6
中,您必须将所有route's
包装在...< ;/Routes>
标签。除了
之外,您不能在...
之间添加任何内容。You are using
react router v6
and inv6
you have to wrap all of yourroute's
in<Routes>...</Routes>
tag.And you can't add anything between
<Routes>...</Routes>
except<Route />
.