Предполагая, что вы знаете, как использовать response-router-dom.если вы не используете эту ссылку, чтобы найти хорошее руководство.
вот пример использования реагирующей маршрутизации с боковой панелью:
import React, { Component } from 'react';
import { ListItemIcon, ListItemText, Divider, IconButton, MenuList, MenuItem, Drawer } from '@material-ui/core';
import { Link, withRouter } from 'react-router-dom';
import { withStyles } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import routes from '../routes/routes';
export class Sidebar extends Component {
constructor(props) {
super(props);
this.activeRoute = this.activeRoute.bind(this);
}
activeRoute(routeName) {
return this.props.location.pathname.indexOf(routeName) > -1 ? true : false;
}
render() {
const { classes, theme } = this.props;
return (
<div>
<Drawer
variant="permanent"
>
<MenuList>
{routes.map((prop, key) => {
return (
<Link to={prop.path} style={{ textDecoration: 'none' }} key={key}>
<MenuItem selected={this.activeRoute(prop.path)}>
<ListItemIcon>
<prop.icon />
</ListItemIcon>
<ListItemText primary={prop.sidebarName} />
</MenuItem>
</Link>
);
})}
</MenuList>
</Drawer>
</div>
);
}
}
export default withRouter(Sidebar);
и файл вашего маршрутизаторадолжно выглядеть так:
import { Home, ContentPaste, Notifications, AccountCircle } from '@material-ui/icons';
import HomePage from '../pages/Home/HomePage';
import ProfilePage from '../pages/Profile/ProfilePage';
const Routes = [
{
path: '/dashboard/home',
sidebarName: 'Home',
navbarName: 'Home',
icon: Home,
component: HomePage
},
{
path: '/dashboard/profile',
sidebarName: 'Profile',
navbarName: 'Profile',
icon: AccountCircle,
component: ProfilePage
}
];
export default Routes;
с помощью activeRoute вы можете выделить текущий маршрут
надеюсь, это поможет вам