[英]react components not rendering with routes
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/
这不起作用,并且不会在“/”或
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;
import React, {useState} from "react"; import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom"; import Users from "./user/pages/Users"; import Profiles from "./Profiles" // this is dummy function App() { const [state, setState] = useState(false) return ( <Router> <Routes> <Route path="/" element={<Users />}/> <Route path="/profiles" element={state ? <Profiles /> : <Navigate to="/" />} /> {/* so you redirect only if your state is false */} </Routes> </Router> ); } export default App;
It is because, You din't provide which element to render in Route
.这是因为,您没有提供要在
Route
中呈现的元素。 It has to be mentioned like element={<Users />}
.必须像
element={<Users />}
一样提及它。 So try like below,所以尝试如下,
import React from "react";
import { BrowserRouter as Router, Route, Routes, Navigate } from "react-router-dom";
import Users from "./user/pages/Users";
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Users />} />
<Navigate to="/" />
</Routes>
</Router>
);
}
export default App;
Based on the docs, all Route components should be placed inside a Routes component, which is nested inside the BrowserRouter.根据文档,所有 Route 组件都应放置在 Routes 组件内,该组件嵌套在 BrowserRouter 内。
Also, I noticed you use Navigate everytime, even when you are already at the index path.另外,我注意到您每次都使用 Navigate,即使您已经在索引路径中。 I think this may cause a problem...
我认为这可能会导致问题...
So, with that in mind, Change your code to因此,考虑到这一点,将您的代码更改为
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Users from "./user/pages/Users";
function App() {
return (
<Router>
<Routes>
<Route path="/">
<Route index element={<Users />} />
</Route>
</Routes>
</Router>
);
}
export default App;
In react-router-dom@6
the Route
component renders its content on the element
prop as a ReactNode
, aka JSX, the only valid child of the Route
component is another Route
component for the case of nesting routes.在
react-router-dom@6
中, Route
组件在element
prop 上将其内容呈现为ReactNode
,也称为 JSX, Route
组件的唯一有效子组件是嵌套路由情况下的另一个Route
组件。 The Users
component should be moved to the element
prop. Users
组件应该移动到element
道具。
Also, to render a redirect to a default route, the Navigate
component should also be rendered on a generic route.此外,要渲染到默认路由的重定向,
Navigate
组件也应该在通用路由上渲染。
Users
component onto route's element
prop.Users
组件移动到路由的element
道具上。Navigate
component into a generic route and pass the replace
prop so issue a redirect.Navigate
组件移动到通用路由中并传递replace
属性,以便发出重定向。 Example:例子:
function App() {
return (
<Router>
<Route path="/" element={<Users />} />
<Route path="*" element={<Navigate to="/" replace />} />
</Router>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.