i am trying to create this to change the status of current while joining the page ..
const navigation = [
{ name: "Dashboard", href: "/dashboard", current: false },
{ name: "Team", href: "/dashboard/team", current: false },
{ name: "Projects", href: "/dashboard/projects", current: false },
{ name: "Applications", href: "/dashboard/applications", current: false },
{ name: "Reports", href: "/dashboard/reports", current: false },
];
for (let i = 0; i < navigation.length; i++) {
if (navigation[i].href === location.pathname) {
navigation[i].current = true;
}
}
i tried to make it with if condition but it’s so messy code and i tried to include the navigation array in every page and change the object from every page but it mess up there’s two page current state being true in the same time :: note : the code is correct but it gives me error when i try to deploy it in vercel to test it with some people
2
Answers
You are using nextjs, that means the first render will happen on the server not the browser so the location and other browser specific globals such as (window, localStorge) will null, the fix is to make you code run on the browser for that wrap your code in a useEffect
NextJs13
If you want highlight active navigation item use custom component like this: