Code To Learn logo

Code To Learn

M6: State ManagementRedux Toolkit Path

L11: Add Navbar to App

Integrate the Navbar into the application layout

Let's integrate the Navbar into our application so it appears on every page!

Current App Structure

Currently, our App.jsx looks like this:

src/App.jsx
import { Provider } from 'react-redux';
import { store } from './state/store';
import { Router } from './components/Router';

export function App() {
  return (
    <Provider store={store}>
      <Router />
    </Provider>
  );
}

The Router component contains all our routes. We need to add the Navbar above the Router so it appears on every page.

Where to Add the Navbar?

We have two options:

Add Navbar in App.jsx:

<Provider store={store}>
  <Navbar />  {/* Shows on all pages */}
  <Router />
</Provider>

Pros:

  • ✅ Navbar outside routing logic
  • ✅ Cleaner separation of concerns
  • ✅ Easier to add other global components

This is what we'll do!

Add Navbar inside Router.jsx:

<BrowserRouter>
  <Navbar />  {/* Inside router */}
  <Routes>
    <Route path="/" element={<HomePage />} />
    {/* ... */}
  </Routes>
</BrowserRouter>

Pros:

  • ✅ All layout in one file

Cons:

  • ❌ Mixes routing with layout
  • ❌ Less flexible

Step 1: Import Navbar

Open src/App.jsx and import the Navbar:

src/App.jsx
import { Provider } from 'react-redux';
import { store } from './state/store';
import { Router } from './components/Router';
import Navbar from './components/Navbar';

export function App() {
  return (
    <Provider store={store}>
      <Router />
    </Provider>
  );
}

Step 2: Add Navbar Component

Place the Navbar above the Router:

src/App.jsx
import { Provider } from 'react-redux';
import { store } from './state/store';
import { Router } from './components/Router';
import Navbar from './components/Navbar';

export function App() {
  return (
    <Provider store={store}>
      <Navbar />
      <Router />
    </Provider>
  );
}

That's it! The Navbar now appears on every page! 🎉

Complete Code

Here's the complete updated App.jsx:

src/App.jsx
import { Provider } from 'react-redux';
import { store } from './state/store';
import { Router } from './components/Router';
import Navbar from './components/Navbar';

export function App() {
  return (
    <Provider store={store}>
      <Navbar />
      <Router />
    </Provider>
  );
}

Only 2 lines added! Simple and clean. ✨

Understanding the Layout Structure

The component tree now looks like:

App
 └─ Provider (Redux store)
     ├─ Navbar (always visible)
     └─ Router
         ├─ HomePage (route: /)
         ├─ ListingDetailsPage (route: /listings/:id)
         ├─ FavoritesPage (route: /favorites)
         └─ NotFoundPage (route: /*)

Key point: Navbar is outside the Router, so it persists across all routes!

Verify the Integration

Let's test the Navbar:

Check HomePage

Navigate to / - you should see:

  • Navbar at the top
  • HomePage content below

The Navbar should stay fixed at the top!

Check Favorites Page

Click the "Favorites" link in the Navbar.

You should:

  • Navigate to /favorites
  • See the Navbar (still there!)
  • See the FavoritesPage content

Check Count Updates

Let's verify the count badge updates:

  1. Open Redux DevTools
  2. Manually dispatch:
    { "type": "listings/toggleFavorite", "payload": 1 }
  3. Watch the Navbar badge - it should show "1"!
  4. Dispatch again to remove - badge should disappear!

Perfect! The Navbar is connected to Redux! 🎉

Common Layout Patterns

Current layout (what we have):

export function App() {
  return (
    <Provider store={store}>
      <Navbar />
      <Router />
    </Provider>
  );
}

Simple and clean! Navbar + content.

Wrap content in layout container:

export function App() {
  return (
    <Provider store={store}>
      <div className="min-h-screen flex flex-col">
        <Navbar />
        <main className="flex-1">
          <Router />
        </main>
      </div>
    </Provider>
  );
}

Benefits:

  • min-h-screen - Full viewport height
  • flex-1 - Content fills available space
  • Proper semantic HTML (<main>)

Complete layout with footer:

import Footer from './components/Footer';

export function App() {
  return (
    <Provider store={store}>
      <div className="min-h-screen flex flex-col">
        <Navbar />
        <main className="flex-1 bg-gray-50">
          <Router />
        </main>
        <Footer />
      </div>
    </Provider>
  );
}

Production-ready layout!

  • Full height
  • Content fills space
  • Background color
  • Footer at bottom

Improving the Layout

Let's add a container and proper spacing:

src/App.jsx
import { Provider } from 'react-redux';
import { store } from './state/store';
import { Router } from './components/Router';
import Navbar from './components/Navbar';

export function App() {
  return (
    <Provider store={store}>
      <div className="min-h-screen flex flex-col">
        <Navbar />
        <main className="flex-1">
          <Router />
        </main>
      </div>
    </Provider>
  );
}

What changed?

Testing Navigation

The Navbar should now work perfectly:

  1. Click "Home" in Navbar
  2. URL changes to /
  3. HomePage displays
  4. No page reload!
  1. Click "Favorites" in Navbar
  2. URL changes to /favorites
  3. FavoritesPage displays
  4. Count badge visible (if you have favorites)
  1. Click "StayScape" logo
  2. Returns to homepage
  3. Common pattern!

What's Next?

Perfect! The Navbar is integrated. In the next lesson, we'll:

  1. Update Router - Add the /favorites route
  2. Test routing - Verify favorites page works
  3. Connect everything - Complete the navigation flow

✅ Lesson Complete! The Navbar is now integrated and appears on every page!

Key Takeaways

  • Add Navbar in App.jsx - Shows on all pages
  • Place above Router - Navbar persists across routes
  • Use semantic HTML - <main> for content
  • Flexbox layout - Proper spacing and alignment
  • min-h-screen - Full viewport height
  • Only 2 lines added - Simple integration!