I have a navbar with five nav links, but when clicked, it does not redirect to the link or position on the website.
This is my code for the header:
const Header = () => {
const [active, setActive] = useState("nav_menu")
const [icon, setIcon] = useState("nav_toggler")
const navToggle = () => {
if (active === "nav_menu") {
setActive("nav_menu nav_active")
} else setActive("nav_menu")
if (icon === "nav_toggler") {
setIcon("nav_toggler")
} else setIcon("nav_toggler")
}
return (
<>
<header>
<div className='headerContainer flexSB'>
<div className='logo'>
<img src='./images/my_logo.png' alt='' />
</div>
<nav>
<ul className={active}>
<li>
<a href='/'>Home</a>
</li>
<li>
<a href='/'>About</a>
</li>
<li>
<a href='/'>Portfolio</a>
</li>
<li>
<a href='/'>News</a>
</li>
<li>
<a href='/'>Contact</a>
</li>
<button className='outline-btn'>BUY NOW</button>
</ul>
</nav>
<div onClick={navToggle} className={icon}>
<i className='fas fa-bars'></i>
</div>
</div>
</header>
</>
)
}
export default Header
It just stays on the website and refreshes when I try to navigate through the links.
Can anyone help me with this?
2
Answers
Remove the anchor tags from your navigation menu.
if you wanna navigate page in reactjs , you need install react-router-dom .
and you dont need tag in reactjs , after insstal react-router-dom
use .
this npm for react-router-dom :
react-router-dom doc :
https://reactrouter.com/en/main
sampel :
in App.jsx first import :
then use them like this :
in element set your component in path set your address you want be navigate
if you want set home page address :
if you want create 404 page set this :
in Header Componet :
import Link :