Medium Com SWLH Creating Sidebars With React Router 64d7f170ca0a
Medium Com SWLH Creating Sidebars With React Router 64d7f170ca0a
You have 2 free stories left this month. Sign up and get an extra one for free.
React is a librar y for creating front end views. It has a big ecosystem of libraries that
work with it. Also, we can use it to enhance existing apps.
To build single-page apps, we have to have some way to map URLs to the React
component to display.
In this article, we’ll look at how to create sidebars with route content only
displaying in a portion of the screen with React Router.
Creating Sidebar
We can easily create a fixed sidebar on one side of the screen while displaying route
content on the other.
To do this, we just need to add our sidebar on one side with links and the Switch and
Route components on the other.
For example, we can write the following code to achieve this effect:
const routes = [
{
path: "/",
exact: true,
sidebar: () => <div>home</div>,
main: () => <h2>Home</h2>
},
{
path: "/foo",
sidebar: () => <div>foo</div>,
main: () => <h2>Bubblegum</h2>
},
{
path: "/shoelbaraces",
sidebar: () => <div>bar</div>,
main: () => <h2>Shoelaces</h2>
}
];
function App() {
return (
<Router>
<div style={{ display: "flex" }}>
<div
style={{
padding: "10px",
width: "40%",
background: "pink"
}}
>
<ul style={{ listStyleType: "none", padding: 0 }}>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/foo">Foo</Link>
</li>
<li>
<Link to="/bar">Bar</Link>
</li>
</ul>
<Switch>
{routes.map((route, index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
children={<route.sidebar />}
/>
))}
</Switch>
</div>
In the code above, we have the sidebar in the top of App . And we have the routes
array as follows:
const routes = [
{
path: "/",
exact: true,
sidebar: () => <div>home</div>,
main: () => <h2>Home</h2>
},
{
path: "/foo",
sidebar: () => <div>foo</div>,
main: () => <h2>Bubblegum</h2>
},
{
path: "/shoelbaraces",
sidebar: () => <div>bar</div>,
main: () => <h2>Shoelaces</h2>
}
];
In the outermost div of App , we have display: 'flex' style to display the sidebar
and the route content side by side, with the sidebar of the left and the route content
on the right.
In the code above, we have the ul element to display the Link s, which we can click
on the show the route that we want.
In the Switch component, we have the routes array mapped to Route s that we
want to display in the sidebar.
We want to display the component we set as the value of the sidebar in each entr y
of routes . Therefore, we set children to that.
In the routes array, we have the route with path set to '/' has the exact property
set to true .
We have to do this so that React Router won’t direct all routes that start with a / to
the ‘home’ route. This is because React Router doesn’t look at the other routes once
it found a match.
If the match isn’t exact, then it’ll take anything that starts with a / as the correct
match. In this case, it’ll be the first route.
The code above maps the routes array again to Route s, but we set the children
Conclusion
We can create a sidebar easily by using flexbox and the Switch and Route
components.
As long as we have Switch and Route components in only one portion of the screen,
the route content will only be displayed on that part of the screen.
Flexbox lets us display items side by side without hassle.
221
WR ITTEN BY
John Au-Yeung Follow
Web developer. Subscribe to my email list now at http://jauyeung.net/subscribe/ . Follow me on
Twitter at https://twitter.com/AuMayeung
T he Startup Follow
Medium's largest active publication, followed by +709K people. Follow to join our community.
Hoisting In JavaScript
Kirill Chaim Shcherbina