Introducing ReUI:Open-source UI components and apps built with React, Next.js and Tailwind CSS
Browse ReUI

The side bar menu-link can't active when click into the link


The side bar menu-link can't active when click. What are the problems?


Text formatting options
Submit

Replies (5)


You can fix the problem by adding the following JS code to your scripts.bundle.js file. It will add the active class for each place where the URL matches the Menu item.


const url = window.location.href;
const menuItems = document.querySelectorAll(".menu-link");
menuItems.forEach(item => {
if (item.href === url) {
item.classList.add("active");
}
});



I have same problem, each link '<a>' that have a class 'menu-link' does not work on click



You can fix the problem by adding the following JS code to your scripts.bundle.js file. It will add the active class for each place where the URL matches the Menu item.


const url = window.location.href;
const menuItems = document.querySelectorAll(".menu-link");
menuItems.forEach(item => {
if (item.href === url) {
item.classList.add("active");
}
});



Hi,

For classic web applications, this is handled on the server side, not in the frontend.
The menu-link active state should be rendered in server-side by placing a special class to highlight the menu links. So please refer to the HTML markup and classes to add login in your server-side to highlight active menu links when you generate pages.

Regards.



I wish you would try to help people a little bit instead of typing fast.


Text formatting options
Submit
Text formatting options
Submit