Created
May 28, 2017 04:24
-
-
Save madcoda/8218dae875c69871d2ced69138042de0 to your computer and use it in GitHub Desktop.
React-router v4 multi layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import { BrowserRouter as Router, Route, Link, Match, Redirect, Switch } from 'react-router-dom' | |
import OverviewPage from './page/OverviewPage' | |
import AccountPage from './page/AccountPage' | |
/* | |
Layouts, inline define here for demo purpose | |
you may want to define in another file instead | |
*/ | |
const DashboardLayout = ({children, ...rest}) => { | |
return ( | |
<div className="page page-dashboard"> | |
<div className="sidebar">Sidebar here</div> | |
<div className="main">{children}</div> | |
</div> | |
) | |
} | |
const LoginLayout = ({children, ...rest}) => { | |
return ( | |
<div className="page page-login"> | |
<div className="main">{children}</div> | |
</div> | |
) | |
} | |
/* | |
Route wrapper | |
*/ | |
const DashboardRoute = ({component: Component, ...rest}) => { | |
return ( | |
<Route {...rest} render={matchProps => ( | |
<DashboardLayout> | |
<Component {...matchProps} /> | |
</DashboardLayout> | |
)} /> | |
) | |
}; | |
const LoginLayoutRoute = ({component: Component, ...rest}) => { | |
return ( | |
<Route {...rest} render={matchProps => ( | |
<LoginLayout> | |
<Component {...matchProps} /> | |
</LoginLayout> | |
)} /> | |
) | |
}; | |
/* | |
App | |
*/ | |
class App extends Component { | |
render() { | |
return ( | |
<Router> | |
<Switch> | |
<Route exact path="/"> | |
<Redirect to="/login" /> | |
</Route> | |
<LoginLayoutRoute path="/login" component={LoginPage} /> | |
<DashboardRoute path="/overview" component={OverviewPage} /> | |
<DashboardRoute path="/account" component={AccountPage} /> | |
</Switch> | |
</Router> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With this approach its gonna hurt the performance the whole gonna render every time the route matched