Sidebar and header
- React course
- Intermediate
- Lesson 50/80
- 4 min
Learn to set up a page layout with a sidebar and header.
Sidebar and header
Many apps have a sidebar on the left and a header on top. These two components sit side by side inside the layout; the content area fills the remaining space.
The sidebar usually holds a list of links; the Link component navigates between pages. The header carries the logo and a user menu.
CSS Flexbox or Grid are common choices for the layout; the sidebar gets a fixed width, the content a flexible width.
Header content
The header usually carries the app logo, a search box and a user menu. It has a fixed height and sits beside the sidebar.
Making the header a separate component makes it easy to swap inside the layout. If you want different headers on different pages, the layout can take the header as a prop.
On mobile, the header often has a menu button that opens the sidebar; this is part of responsive design.
Practice Sidebar component
Complete the skeleton below. Write a Sidebar component; return a nav containing <Link to="/">Home</Link> and <Link to="/products">Products</Link>. Export it.
Starter code
import { Link } from "react-router-dom";
// Write the Sidebar component hereSample solution
import { Link } from "react-router-dom";
function Sidebar() {
return (
<nav>
<Link to="/">Ana sayfa</Link>
<Link to="/products">Ürünler</Link>
</nav>
);
}
export default Sidebar;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is usually in the sidebar?
- A form field
- Tablo
- A list of links Correct answer
- Modal
Explanation: The sidebar holds a list of links for navigation.
2 Sidebar link
import { Link } from "react-router-dom";
<nav><____ to="/">Ana sayfa</Link></nav>
import { Link } from "react-router-dom";
<nav><Link to="/">Ana sayfa</Link></nav>
Explanation: Link to provides a page link.
3 Sidebar and header: match the terms
| Term | Meaning |
|---|---|
Sidebar |
Left menu |
Header |
Top header |
Link |
Page link |
main |
Content area |