-1
const Navbar = () => {
  return (
    <div>
      {location === '/' ? (
        <AuthNav />
      ) : location === '/home' && isAuthenticated ? (
        <MainNav />
      ) : <AuthNav />
      }
    </div>
  );
};

How do I render two separate navbars on different application routes, in this case, I want to render the AuthNav in the login and signup path and I want to render MainNav on the home path.

Dave
  • 5,108
  • 16
  • 30
  • 40
  • Hey, could you edit your post and format your code correctly? Thanks! – Sean Anglim Dec 04 '22 at 15:52
  • Are you using `react-router-dom@6`? Does this answer your question? https://stackoverflow.com/a/69999387/8690857 – Drew Reese Dec 04 '22 at 18:52
  • @DrewReese Yes, when are you avaliable so I can show the code. Also, I am using react router dom – Bj Redding Dec 05 '22 at 02:16
  • I'm just about available all the time, but you should include all the relevant code you are working with and have an issue using in your question. – Drew Reese Dec 05 '22 at 03:52
  • @DrewReese Ok so why does my react app only return one navbar instead of two different one on different routes? For example, in the signup/login paths I only want to show AuthNav and when they user logs in I want to show Main Navbar. Do you have discord to further see my code. – Bj Redding Dec 05 '22 at 15:51
  • The answer I linked to explains how to render different components with different routes. Perhaps it might be easier to see where you are getting stuck if you could edit your post to include a complete [mcve] for how/where you are rendering this `Navbar` component and the routes and components. – Drew Reese Dec 05 '22 at 17:30
  • @DrewReese Sure you can take a look at my code in https://github.com/Brian-Tech-20s/Music-Essentials/tree/working-branch – Bj Redding Dec 06 '22 at 11:00

1 Answers1

0

Issues

I think you've a few things working against you:

  1. The Navbar component is unconditionally rendered and using window.location.pathname to compute which actual navigation component to render. This means the view to be rendered is only computed when the Navbar component rerenders.
  2. The Navbar component is rendered outside the Routes, so it's not rerendered when a route changes.

Solution

Instead of unconditionally rendering Navbar and trying to compute which nav component to render based on any current URL pathname, split them out into discrete layout routes that render the appropriate nav component.

Example:

Navbar.jsx

export const AuthNav = ({ auth }) => {
  ....
};

export const MainNav = () => {
  ....
};

App.jsx

import { Routes, Route, Navigate, Outlet } from 'react-router-dom';
import { useState } from "react";

// components
import { AuthNav, MainNav } from './components/Navbar';

// pages
...

...

const AuthLayout = ({ auth }) => (
  <>
    <AuthNav auth={auth} />
    <Outlet />
  </>
);

const MainLayout = () => (
  <>
    <MainNav />
    <Outlet />
  </>
);

const PrivateRoute = ({ auth }) => {
  return auth.isAuthenticated
    ? <Outlet />
    : <Navigate to="/" replace />;
};
const App = () => {
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  return (
    <div className='parent'>
      <Routes>
        <Route element={<AuthLayout auth={{ isAuthenticated, setIsAuthenticated }} />}>
          <Route path='/' element={<SignIn />} />
          <Route path='/signup' element={<SignUp />} />
        </Route>
        <Route element={<MainLayout />}>
          <Route element={<PrivateRoute auth={{ isAuthenticated }} />}>
            <Route path='/Home' element={<Home />} />
            <Route path='/music' element={<Music />} />
            <Route path='/genre/' element={<Pop />} />
            <Route path='/Hiphop' element={<HipHop />} />
            <Route path='/Rock' element={<Rock />} />
            <Route path='/EDM' element={<EDM />} />
            <Route path='/Jazz' element={<Jazz />} />
            <Route path='/RandB' element={<RandB />} />
            <Route path='/store' element={<Store />} />
            <Route path='/News' element={<News />} />
            <Route path='/Contact' element={<Contact />} />
            <Route path='/album/:id' element={<Album />} />
            <Route path ="/album/:id/nested/" element={<Albums2 />} />
          </Route>
        </Route>
      </Routes>
    </div>
  );
};

Edit issue-only-renders-one-navbar-instead-of-two-navbars-in-reactjs

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • Ok, I see how can I export the object of AuthNav and MainNav in my navbar.jsx file? and how should my code look in Navbar.jsx? Here is link to file: https://github.com/Brian-Tech-20s/Music-Essentials/blob/working-branch/src/components/Navbar.jsx. We are getting closer I just realize that my routes were wrong for the navbar. – Bj Redding Dec 08 '22 at 14:52
  • @BjRedding Sorry, it was obvious in my mind when answering but I should have been more explicit; the `Navbar` is removed since it's not necessary, the *actual* navigation components are exported/imported and rendered now in the individual layout route components. – Drew Reese Dec 08 '22 at 17:17
  • can you please show an example of this? I am getting an error of AuthNav needs to be export with all the code you have. – Bj Redding Dec 08 '22 at 19:27
  • @BjRedding Sure. They just need to be declared and exported. I've updated answer. – Drew Reese Dec 08 '22 at 20:10
  • Resse Ok everything close but how do I display the MainNav bar in the home page? – Bj Redding Dec 09 '22 at 16:23
  • @BjRedding The home page `Home` is rendered on `"/Home"` route rendered in the `MainLayout` layout route that renders the `MainNav` component. `MainNav` *should* be rendered when the home page route is matched and rendered. Is this not the case for you currently? – Drew Reese Dec 09 '22 at 18:27
  • Resse No it does not display in the home page route, do I call the MainLayout on the home page to display it? Also, there is no navbar that displays but there are no errors which is a good sign. – Bj Redding Dec 10 '22 at 11:50
  • @BjRedding Code works expected here in a running [codesandbox](https://codesandbox.io/s/issue-only-renders-one-navbar-instead-of-two-navbars-in-reactjs-h1wnog). You might be missing something small. Feel free to fork my sandbox and edit it to match what you are running and see if you can create a reproducible example we can inspect live. – Drew Reese Dec 10 '22 at 21:23
  • Resse Thanks for the advice, but I actually use this website to figure out my logic of how I wanted it and it works. Reference: https://stackoverflow.com/questions/63041349/change-navbar-on-homepage-when-using-react-router-dom – Bj Redding Dec 13 '22 at 07:11
  • @BjRedding What exactly is the issue that remains/remained? – Drew Reese Dec 13 '22 at 07:15