[英]React router dom not redirect correctly
That's my code, when i try to go in / or /home the components are rendered, but when i want to go in /login or /contacts it doesn't render, i don't know where is the problem, can someone help me?这是我的代码,当我尝试在 / 或 /home 中使用 go 时,组件会被渲染,但是当我想在 /login 或 /contacts 中使用 go 时,它不会呈现,我不知道问题出在哪里,谁能帮忙我?
import React, { Component } from "react"
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
import ContentInfo from './components/Card/Card';
import LoginCard from './components/LoginCard/LoginCard';
import ContactsCard from './components/ContactsCard/ContactsCard';
import RegisterCard from './components/RegisterCard/RegisterCard';
import {Root} from './components/Root';
import {HomeRoot} from './components/HomeRoot';
import Panel from './components/Panel/Panel';
import './components/particles.css';
class App extends Component {
render(){
return (
<Router>
<Switch>
<Route path={"/"} exact>
<Root>
<Route component={ContentInfo} />
<Route path={"/login"} exact component={LoginCard} />
<Route path={"/contacts"} component={ContactsCard} />
<Route path={"/register"} component={RegisterCard} />
</Root>
</Route>
<Route path={"/home"}>
<Switch>
<HomeRoot>
<Route component={Panel} />
</HomeRoot>
</Switch>
</Route>
</Switch>
</Router>
);
}
}
export default App;
From our comment conversation if I understand correctly you are looking to do the following:如果我理解正确,从我们的评论对话中,您希望执行以下操作:
import { useLocation } from 'react-router-dom'
.
.
.
const location = useLocation()
return (
<Router>
<Switch>
{location.pathname === '/home' ? <HomeRoot /> : <Root />}
<Route exact path={"/"} component={ContentInfo} />
<Route path={"/login"} component={LoginCard} />
<Route path={"/contacts"} component={ContactsCard} />
<Route path={"/register"} component={RegisterCard} />
<Route path={"/home"} component={Panel}>
</Route>
</Switch>
</Router>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.