Last active
December 8, 2019 14:15
-
-
Save henev/6e537309397134861ff4941186f4e297 to your computer and use it in GitHub Desktop.
[React Router Layouts] Main file
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 from 'react'; | |
import { BrowserRouter, Route, Link, Switch } from 'react-router-dom'; | |
import { render } from 'react-dom'; | |
import { One, Two, Three, Four } from './Components'; | |
import { LayoutOne, LayoutTwo } from './Layouts'; | |
function App() { | |
return ( | |
<BrowserRouter> | |
<h3>Content of main App component</h3> | |
<ul> | |
<li><Link to="/one">One</Link></li> | |
<li><Link to="/two">Two</Link></li> | |
<li><Link to="/three">Three</Link></li> | |
<li><Link to="/four">Four</Link></li> | |
</ul> | |
<Switch> | |
<RouteWrapper path="/one" component={One} layout={LayoutOne} /> | |
<RouteWrapper path="/two" component={Two} layout={LayoutOne} /> | |
<RouteWrapper path="/three" component={Three} layout={LayoutTwo} /> | |
<RouteWrapper path="/four" component={Four} layout={LayoutTwo} /> | |
</Switch> | |
</BrowserRouter> | |
); | |
} | |
function RouteWrapper({ | |
component: Component, | |
layout: Layout, | |
...rest | |
}) { | |
return ( | |
<Route {...rest} render={(props) => | |
<Layout {...props}> | |
<Component {...props} /> | |
</Layout> | |
} /> | |
); | |
} | |
render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment