反应组件不使用路线渲染
我正在使用最新版本的React路由器。当我在组件中使用路线时,它们不会渲染任何内容,但是当我删除路线并简单地使用它们正常工作的组件时。无法理解出什么问题,
这是不起作用的,并且没有在“/”或http:// localhost上渲染任何内容:3000/
import React from "react";
import { BrowserRouter as Router, Route, Navigate } from "react-router-dom";
import Users from "./user/pages/Users";
function App() {
return (
<Router>
<Route path="/" exact>
<Users />
</Route>
<Navigate to="/" />
</Router>
);
}
export default App;
这是渲染且正常工作的。
import React from "react";
import { BrowserRouter as Router, Route, Navigate } from "react-router-dom";
import Users from "./user/pages/Users";
function App() {
return <Users />;
}
export default App;
I am using the latest version of react router. When I am using routes in my component, They are not rendering anything but When I remove the routes and use simply the component they are working fine. Not able to understand what is going wrong
This do not work and is not rendering anything on "/" or http://localhost:3000/
import React from "react";
import { BrowserRouter as Router, Route, Navigate } from "react-router-dom";
import Users from "./user/pages/Users";
function App() {
return (
<Router>
<Route path="/" exact>
<Users />
</Route>
<Navigate to="/" />
</Router>
);
}
export default App;
This is rendering and working fine.
import React from "react";
import { BrowserRouter as Router, Route, Navigate } from "react-router-dom";
import Users from "./user/pages/Users";
function App() {
return <Users />;
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为,您不提供在
路由
中渲染的元素。必须像element = {&lt;用户 /&gt;} < /code>一样提及。因此,请尝试下面
It is because, You din't provide which element to render in
Route
. It has to be mentioned likeelement={<Users />}
. So try like below,基于文档,所有路由组件都应放置在嵌套在浏览器内的路由组件中。
另外,我注意到您每次都使用导航,即使您已经处于索引路径。我认为这可能会导致问题...
因此,考虑到这一点,将您的代码更改为
Based on the docs, all Route components should be placed inside a Routes component, which is nested inside the BrowserRouter.
Also, I noticed you use Navigate everytime, even when you are already at the index path. I think this may cause a problem...
So, with that in mind, Change your code to
react-router-dom@6
route
组件在element
prop prop op areact> react node
prop上呈现其内容,又称JSX,路由
组件的唯一有效的孩子是另一个route
组件的组件。用户
组件应移至元素
prop。另外,要将重定向到默认路由,
导航
组件应 在通用路由上渲染。解决方案
用户
组件移动到路由的元素
prop。导航
组件移动到通用路由中,然后传递替换
prop,请发出重定向。例子:
Issue
In
react-router-dom@6
theRoute
component renders its content on theelement
prop as aReactNode
, a.k.a. JSX, the only valid child of theRoute
component is anotherRoute
component for the case of nesting routes. TheUsers
component should be moved to theelement
prop.Also, to render a redirect to a default route, the
Navigate
component should also be rendered on a generic route.Solution
Users
component onto route'selement
prop.Navigate
component into a generic route and pass thereplace
prop so issue a redirect.Example: