Go to https://reactrouter.com/web/example/nesting, then replace the code example for the nesting case with this code.
Created
June 4, 2021 03:52
-
-
Save ifindev/1d1e33acea825d0ec69f4963af98dd79 to your computer and use it in GitHub Desktop.
Side Navbar with React Router for Dashboard
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 as Router, | |
Switch, | |
Route, | |
Link, | |
useParams, | |
useRouteMatch | |
} from "react-router-dom"; | |
// Since routes are regular React components, they | |
// may be rendered anywhere in the app, including in | |
// child elements. | |
// | |
// This helps when it's time to code-split your app | |
// into multiple bundles because code-splitting a | |
// React Router app is the same as code-splitting | |
// any other React app. | |
export default function NestingExample() { | |
return ( | |
<Router> | |
<div style={{display:"flex", minHeight:"100vh"}}> | |
<div style={{ | |
width:"100px", | |
backgroundColor:"#eeee", | |
}}> | |
<ul> | |
<li> | |
<Link to="/">Home</Link> | |
</li> | |
<li> | |
<Link to="/topics">Topics</Link> | |
</li> | |
</ul> | |
</div> | |
<div style={{ | |
flex:"auto", | |
backgroundColor:"#f5e6ca", | |
}}> | |
<Switch> | |
<Route exact path="/"> | |
<Home /> | |
</Route> | |
<Route path="/topics"> | |
<Topics /> | |
</Route> | |
</Switch> | |
</div> | |
</div> | |
</Router> | |
); | |
} | |
function Home() { | |
return ( | |
<div> | |
<h2>Home</h2> | |
</div> | |
); | |
} | |
function Topics() { | |
// The `path` lets us build <Route> paths that are | |
// relative to the parent route, while the `url` lets | |
// us build relative links. | |
let { path, url } = useRouteMatch(); | |
return ( | |
<div style={{ | |
display:"flex", | |
height:"100%" | |
}}> | |
<div style={{ | |
backgroundColor:"#deedf0", | |
width:"300px" | |
}}> | |
<h2>Topics</h2> | |
<ul> | |
<li> | |
<Link to={`${url}/rendering`}>Rendering with React</Link> | |
</li> | |
<li> | |
<Link to={`${url}/components`}>Components</Link> | |
</li> | |
<li> | |
<Link to={`${url}/props-v-state`}>Props v. State</Link> | |
</li> | |
</ul> | |
</div> | |
<div style={{ | |
flex:"auto", | |
backgroundColor:"#fff5eb" | |
}}> | |
<Switch> | |
<Route exact path={path}> | |
<h3>Please select a topic.</h3> | |
</Route> | |
<Route path={`${path}/:topicId`}> | |
<Topic /> | |
</Route> | |
</Switch> | |
</div> | |
</div> | |
); | |
} | |
function Topic() { | |
// The <Route> that rendered this component has a | |
// path of `/topics/:topicId`. The `:topicId` portion | |
// of the URL indicates a placeholder that we can | |
// get from `useParams()`. | |
let { topicId } = useParams(); | |
return ( | |
<div> | |
<h3>{topicId}</h3> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment