Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 21 of the Intermediate React: Hooks, State & Data Patterns course
ReactJune 26, 20263 min read

Building the Dashboard Navigation Structure with React Router

Learn how to build a robust dashboard navigation UI by integrating sidebar layouts, private route guards, and authentication state in React.

ReactReact RouterUIDashboardNavigationjavascriptfrontend

Previously in this course, we explored the mechanics of Introduction to React Router: Building Multi-Page SPAs and learned how to secure specific views using Protected Routes for Authenticated Views in React. Now, we will synthesize these concepts into a production-ready dashboard shell.

A professional dashboard isn't just a collection of pages; it’s a cohesive navigation experience. Our goal today is to build a persistent sidebar layout that remains visible while the main content area changes, all while ensuring that our navigation links reflect the user's current authentication state.

Architecting the Dashboard Layout

When building a dashboard, you want a persistent layout. Instead of re-rendering the sidebar on every page transition, we use Nested Routes and Layouts: Mastering React Router UI Design to wrap our sub-pages.

The layout component acts as the "shell." It contains the static UI—like your sidebar and top navigation—and uses the <Outlet /> component to render the specific page content.

The Sidebar Component

The sidebar needs to be aware of the current route to highlight active links. We use the NavLink component from react-router-dom, which automatically applies an active class to the link matching the current URL.

JSX
import { NavLink } from CE9178">'react-router-dom';

const Sidebar = () => {
  return (
    <nav className="sidebar">
      <ul>
        <li><NavLink to="/dashboard/overview">Overview</NavLink></li>
        <li><NavLink to="/dashboard/settings">Settings</NavLink></li>
      </ul>
    </nav>
  );
};

Connecting Auth to Navigation

A common requirement in dashboard UI development is showing different menu items based on the user's session. Since we have already implemented Handling Authentication State with React Context API, we can consume that state directly in our navigation.

Here is how we integrate the Auth state into our layout:

JSX
import { Outlet } from CE9178">'react-router-dom';
import { useAuth } from CE9178">'../context/AuthContext';
import Sidebar from CE9178">'./Sidebar';

export const DashboardLayout = () => {
  const { user, logout } = useAuth();

  return (
    <div className="dashboard-container">
      <Sidebar />
      <main className="content">
        <header>
          <span>Welcome, {user?.name}</span>
          <button onClick={logout}>Logout</button>
        </header>
        {/* The child routes render here */}
        <Outlet />
      </main>
    </div>
  );
};

Defining Private Dashboard Routes

With the layout in place, we define our route configuration. We want to ensure that all /dashboard/* routes are protected. By nesting these inside a parent route that uses our ProtectedRoute component, we centralize the security logic.

JSX
<Routes>
  <Route element={<ProtectedRoute />}>
    <Route path="/dashboard" element={<DashboardLayout />}>
      <Route path="overview" element={<Overview />} />
      <Route path="settings" element={<Settings />} />
    </Route>
  </Route>
</Routes>

By placing the DashboardLayout inside the ProtectedRoute, the layout itself only renders if the user is authenticated. This prevents the sidebar from flickering or rendering for unauthorized users.

Hands-on Exercise: Implementing the "Logout" Redirect

Your task is to refine the DashboardLayout navigation.

  1. Create a LogoutButton component that uses Mastering Programmatic Navigation with useNavigate in React to redirect the user to the login page immediately after the logout() function is called.
  2. Add a conditional check in your Sidebar to only display the "Admin" link if user.role === 'admin'.

Common Pitfalls

  • Forgetting the Outlet: If your sidebar shows up but your pages are blank, double-check that you have included the <Outlet /> component inside your DashboardLayout. Without it, React Router doesn't know where to inject the child components.
  • Over-nesting: While nesting is powerful, try to keep your layout structure flat. If you find yourself passing props through three layers of layouts, consider lifting that state to a Context Provider.
  • Stale Auth State: Ensure your Auth Context is high enough in the tree that it doesn't re-initialize on every route change, which would cause the dashboard to flicker or "flash" the login screen.

Recap

We have successfully moved from basic routing to a structural dashboard pattern. By combining the Outlet for layout persistence, NavLink for active state, and our existing Auth Context for security, we've created a robust foundation. Your dashboard is now a true SPA where navigation feels instant and secure.

Up next: We will begin moving away from static mock data and start managing the Asynchronous Data Lifecycle to fetch real metrics from your API.

Previous lessonProgrammatic NavigationNext lesson Asynchronous Data Lifecycle
Back to Blog

Similar Posts

ReactJune 26, 20263 min read

Mastering Programmatic Navigation with useNavigate in React

Learn how to use the useNavigate hook in React Router to trigger navigation on events, handle redirects, and manage complex user flows in your dashboard.

Read more
ReactJune 26, 20264 min read

Nested Routes and Layouts: Mastering React Router UI Design

Learn how to use nested routes and the Outlet component in React Router to build consistent page layouts, reduce code duplication, and improve UI design.

Part of the course

Intermediate React: Hooks, State & Data Patterns

intermediate · Lesson 21 of 48

  1. 1

    Mastering useRef for DOM Access

    4 min
  2. 2

    Persistent Mutable Values with useRef

    4 min
  3. 3

    Memoizing Expensive Calculations with useMemo

    3 min
Read more
ReactJune 26, 20263 min read

Dynamic Routing with URL Parameters in React Router

Master dynamic routing with React Router. Learn how to define parameterized paths and use the useParams hook to display unique data in your dashboard.

Read more
4

Optimizing Function References with useCallback

3 min
  • 5

    Introduction to Custom Hooks

    4 min
  • 6

    Building a useLocalStorage Hook

    4 min
  • 7

    Refactoring Dashboard Settings

    4 min
  • 8

    Complex State with useReducer

    3 min
  • 9

    Managing Object-Based State

    3 min
  • 10

    Introduction to Context API

    3 min
  • 11

    Architecting Global State with Context and Reducer

    3 min
  • 12

    Implementing Theme Context

    4 min
  • 13

    Structuring State for Performance

    3 min
  • 14

    Handling Authentication State

    3 min
  • 15

    Integrating Reducers with Auth State

    3 min
  • 16

    Introduction to React Router

    3 min
  • 17

    Dynamic Routing with URL Parameters

    3 min
  • 18

    Nested Routes and Layouts

    4 min
  • 19

    Protected Routes for Authenticated Views

    3 min
  • 20

    Programmatic Navigation

    3 min
  • 21

    Building the Dashboard Navigation Structure

    3 min
  • 22

    Asynchronous Data Lifecycle

    3 min
  • 23

    Caching Strategies with React Query

    4 min
  • 24

    Mutations and Data Updates

    4 min
  • 25

    Synchronizing Client and Server State

    Coming soon
  • 26

    Integrating Live Data into the Dashboard

    Coming soon
  • 27

    Error Handling and Loading UI

    Coming soon
  • 28

    Controlled vs Uncontrolled Components

    Coming soon
  • 29

    Real-time Form Validation

    Coming soon
  • 30

    Schema-based Validation with Zod

    Coming soon
  • 31

    Handling Multi-step Forms

    Coming soon
  • 32

    Optimizing Form Submissions

    Coming soon
  • 33

    Performance Profiling with React DevTools

    Coming soon
  • 34

    Refactoring for Scalability

    Coming soon
  • 35

    Finalizing Dashboard Data Flow

    Coming soon
  • 36

    Deploying the Application

    Coming soon
  • 37

    Advanced Hook Composition

    Coming soon
  • 38

    Implementing Middleware for State

    Coming soon
  • 39

    Advanced Context Patterns

    Coming soon
  • 40

    Router Loaders and Data Prefetching

    Coming soon
  • 41

    Complex Route Guards

    Coming soon
  • 42

    Handling Large Datasets in UI

    Coming soon
  • 43

    Testing Hooks and Components

    Coming soon
  • 44

    Managing Global Modals

    Coming soon
  • 45

    Implementing Keyboard Shortcuts

    Coming soon
  • 46

    Optimizing Asset Loading

    Coming soon
  • 47

    Internationalization Basics

    Coming soon
  • 48

    Managing WebSocket Connections

    Coming soon
  • View full course