Design comparison
Solution retrospective
This project focus in an importants points , it makes you deal with many parts like Layout, responsive as well as javascript part ... Really it's an interesting project i like it I have problem i could'nt fix it : when i navigate to other page i need dobble click to active the white border under its name ,How can i fix it Your feedback are welcom
Community feedback
- @EduardIonescuPosted almost 2 years ago
Hi, your solution doesn't work because everytime you go to another page, it triggers a re-render, and since you have the navbar inside each page, it re-renders too. But you can try using
useLocation
to implement it in a simple way. I used something like:import { useLocation } from 'react-router-dom' import Link from "whatever"; export default function Navbar(){ const location= useLocation(); return (<Link to="/destination" className=`${location.pathname == "/destination" && "link-active"}`>...</Link>) }
- Here
location.pathname
will return your route so basically if you're in the Destination page it will return '/destination' and if that is the case, then the condition will be true and return"active-link"
as the className . Therefore if you are on the page that matches the respective link's address, the link will be the only one active so you don't have to worry about onClick events. - I honestly used useRouter from nextjs, but it should be the same as useLocation in react-router.
Alternatively, you could probably keep it the way you did it if you create a layout with the navbar that doesn't change when the page changes, but am not sure about it.
I hope this helped and good luck debugging it haha!
Marked as helpful1@NizarMjrPosted almost 2 years ago@EduardIonescu Thank you for your feedback At all events i get the idea from you and im sure i will fix it as long as there are a lot of hooks i don't know about them like useLocation as you mentionned so i have a long journey and many bugs are waiting xD Thank you and Happy Coding ...
1 - Here
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord