[英]Nested React Router doesn't update layout
App component with Root routes:具有根路由的应用程序组件:
import React, { Component } from 'react';
import './App.css';
import Routes from './routes';
class App extends Component {
render() {
return (
<Routes/>
);
}
}
export default App;
Root routes:根路由:
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Login from './scenes/Login/Login';
import Dashboard from './scenes/Dashboard/Dashboard';
const Routes = () => (
<Router>
<Switch>
<Route exact path="/" component={Login} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</Router>
);
export default Routes;
Nested component with nested routes:带有嵌套路由的嵌套组件:
import React, { Component } from 'react';
import logo from '../../logo.png';
import './Dashboard.css';
import Routes from './routes';
import { Link, withRouter } from 'react-router-dom';
class Dashboard extends Component {
render() {
return (
<div className="App">
<div id="map" />
<div className='sidebar'>
<img src={logo} className="App-logo" alt="logo" />
<Link to={{ pathname: `/dashboard/profile` }}>
<i class="icon blue fa fa-2x fa-user mr-3 float-right"></i>
</Link>
<Link to={{ pathname: `/dashboard/notifications` }}>
<i class="icon blue fa fa-2x fa-bell mr-3 float-right"></i>
</Link>
<Link to={{ pathname: `/dashboard/home` }}>
<i class="icon blue fa fa-2x fa-home mr-3 float-right"></i>
</Link>
<Routes />
</div>
</div>
);
}
}
export default Dashboard;
Nested routes:嵌套路由:
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Notifications from './scenes/Notifications/Notifications';
import Home from './scenes/Home/Home';
import Profile from './scenes/Profile/Profile';
const Routes = () => (
<Router>
<Switch>
<Route path="/dashboard/home" component={Home} />
<Route path="/dashboard/profile" component={Profile} />
<Route path="/dashboard/notifications" component={Notifications} />
</Switch>
</Router>
);
export default Routes;
So the problem is that when I click the Link ex.( /dashboard/notifications
) it changes the url in the browser but doesn't update the layout but when I refresh the page it works fine and the proper components are visible.所以问题是,当我单击链接 ex.(
/dashboard/notifications
) 时,它会更改浏览器中的 url,但不会更新布局,但是当我刷新页面时,它工作正常并且正确的组件可见。 The /
route and dashboard
works fine. /
路线和dashboard
工作正常。
I may be a little late, but passing the location to the switch solved my issue.我可能有点晚了,但是将位置传递给交换机解决了我的问题。
import {
BrowserRouter as Router,
Switch,
Route,
useLocation
} from "react-router-dom";
let location = useLocation();
and inside the render:并在渲染内部:
<Router>
<Switch location={location}>
<Route path="/dashboard/home" component={Home} />
<Route path="/dashboard/profile" component={Profile} />
</Switch>
</Router>
尝试使用 NavLink 标签代替 Link 和 BrowserRouter 代替 Router ,从“react-router-dom”导入
use this method of link syntax使用这种链接语法的方法
<Link to='/dashboard/notifications'>
<i class="icon blue fa fa-2x fa-bell mr-3 float-right"></i>
</Link>
instead of this而不是这个
<Link to={{ pathname: `/dashboard/notifications` }}>
<i class="icon blue fa fa-2x fa-bell mr-3 float-right"></i>
</Link>
if it dosn't work it may something with your browser caching or some other problems如果它不起作用,它可能与您的浏览器缓存或其他一些问题有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.