Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I use multiple navbars in my react application

I’m trying to have a different navbar for each path in my website, i figured out a way but it doesn’t really change until I refresh the page which is obviously not what i want. ( I’m using react router v6.3 )

    function Navbar() {
if (window.location.pathname === "/") {
return (
<nav style={{ backgroundColor: "rgb(17, 105, 142)" }}>...</nav>
);
} else if (window.location.pathname === "/products") {
return (
<nav className="products-nav">...</nav>
);
} else if (window.location.pathname === "/addproducts") {
return (
<nav className="addproducts-nav">...</nav>
);
} else ...

Here‘s the full code just in case

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use useLocation hook from react-router-dom which returns the current location object https://reactrouter.com/en/main/hooks/use-location

import React from 'react'
import useLocatio from 'react-router-dom'

const functionalComponent = () => {
    const location = useLocation();
    console.log(location.pathname)
    return ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading