Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
React way of selecting elements and adding event listeners

I'm building a portfolio site with gatsby where I have a sidebar on the left with some links (Home, about, work, etc.) and on the right there's the main content which is one long strip of sections. Clicking a link simply scrolls to that section. I would like for a link to be styled differently when the user reaches that section. I've found a solution in vanilla javascript to apply active state that works, but I don't know how to do this the react way.

useEffect(() => {
const navbarlinks = document.querySelectorAll("nav a");

const navbarlinksActive = () => {
  let position = window.scrollY + 200;
  navbarlinks.forEach((navbarlink) => {
    let section = document.querySelector(navbarlink.hash);
    if (position >= section.offsetTop && position <= section.offsetTop + section.offsetHeight) {
      navbarlink.classList.add("active");
    } else {
      navbarlink.classList.remove("active");
    }
  });
};

window.addEventListener("load", navbarlinksActive);
document.addEventListener("scroll", navbarlinksActive);

return () => {
  window.removeEventListener("load", navbarlinksActive);
  document.removeEventListener("scroll", navbarlinksActive);
};
}, []);

I'm selecting all the nav links, then looping through them, getting the hash property, which will match the id of the corresponding section which is then selected. Then the class is added or removed based on its position. This code is in the sidebar component where my links, which are gatsby Link elements, also are.

return (    
  <nav>
    <Link
      className="hover:text-clr-accent flex items-center py-2 px-4 transition"
      to="#home"
    >
      <i className="fa-solid fa-house w-6 text-center text-lg"></i>
      <span className="pl-2 text-lg">Home</span>
    </Link>

    <Link
      className="hover:text-clr-accent flex items-center py-2 px-4 transition"
      to="#resume"
    >
      <i className="fa-solid fa-file w-6 text-center text-lg"></i>
      <span className="pl-2 text-lg">Resume</span>
    </Link>

    <Link
      className="hover:text-clr-accent flex items-center py-2 px-4 transition"
      to="#about"
    >
      <i className="fa-solid fa-address-card w-6 text-center text-lg"></i>
      <span className="pl-2 text-lg">About me</span>
    </Link>

    <Link
      className="hover:text-clr-accent flex items-center py-2 px-4 transition"
      to="#work"
    >
      <i className="fa-solid fa-briefcase w-6 text-center text-lg"></i>
      <span className="pl-2 text-lg">My work</span>
    </Link>
  </nav>
);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. Instead of directly adding/removing the active class set the active link Id to state and render based on that. Otherwise react may overwrite your classes by rendering

  2. Don't use the load event listener instead just call navbarlinksActive(), the window is already loaded by the time your effect is called.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!