Вы должны поместить каждый компонент в тег 'layout'.Но если у вас есть много маршрутов для размещения, то лучше всего сделать что-то вроде этого:
import * as React from 'react';
import { Route, Redirect, withRouter } from 'react-router-dom';
import SessionManager from '../../../session/session-manager';
class AppRoute extends React.Component<any> {
/**
* Constructor
*
* @param {*} props
* @memberof AppRoute
*/
constructor(props: any) {
super(props);
}
/**
* Component will mount callback
*
* @memberof AppRoute
*/
public componentWillMount(): void {
SessionManager.getInstance().loadSession();
}
/**
* Render
*
* @returns {JSX.Element}
* @memberof AppRoute
*/
public render(): JSX.Element {
const { private: isPrivate, layout: Layout, component: Component, ...rest } = this.props;
if (isPrivate === true) {
return (
<Route
{...rest}
render={props => SessionManager.getInstance().isValidSession() ? (<Layout><Component {...props} /></Layout>) : (<Redirect to={{ pathname: '/login', state: { from: this.props.location } }} />)}
/>
)
} else {
return (
<Route {...rest} render={props => (
<Layout>
<Component {...props} />
</Layout>
)} />
)
}
}
}
export default withRouter(AppRoute);
В моем случае я воспользовался возможностью добавить защиту маршрута.
Наконец:
<Switch>
<Route exact={true} path="/login" component={LoginModule} />
<AppRoute exact={true} path="/" layout={MainLayout} private={true} component={DashboardModule} />
<AppRoute exact={true} path="/dashboard" layout={MainLayout} private={true} component={DashboardModule} />
<AppRoute exact={true} path="/players" layout={MainLayout} private={true} component={PlayersModule} />
<Route component={NotFound} />
</Switch>
С уважением.