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

199
Views
Why I can't scroll a div into view?

So I have a sidebar, and when I click on one of the items, it should scroll to div with a certain name. It worked before, but I decided to add 'active' class to the item clicked to change its color, and it stopped scrolling to divs.

Sidebar.jsx file:

import React, { useState} from 'react';
import './Sidebar.scss';
import { SlidebarData } from "./SlidebarData";
import spike from '../../assets/spike_pic.jpg';

const Sidebar = () => {
    const [sideBar, setSidebar] = useState(false);
    const [selectedLink, setSelectedLink] = useState("");

    return (
        <div className="sidebar">
        <span className="btn" onClick={() => setSidebar(!sideBar)}>Menu</span>
            <div className="profile">
                <img src={spike}/>
                <span>Alim Budaev</span>
                <span>Available for work</span>
            </div>
            <ul className="sidebarlist" id={sideBar ? "hidden" : ""}>
            {SlidebarData.map((val,key) =>{
                return (
                <li 
                    className={`row ${selectedLink === val.link ? "active" : ""}`}
                    id={val.link}
                    key={key} 
                    onClick={()=> {
                        setSelectedLink(val.link);
                        document.getElementById(val.link).scrollIntoView();  
                    }}>
                    {""}
                    <div>
                        {val.title}
                    </div>
                    </li>
                );
            })}
            </ul>
        </div>
    );
}

export default Sidebar;

As you see, I have a setState hook for selectedlink, which adds active class, but scrollIntoView doesn't work because of it, and I can't figure out why.

SidebarData.jsx, where I get links from:

export const SlidebarData = [
    {
        title: "Home",
        link: "home"
    },
    {
        title: "About",
        link: "about"
    },
    {
        title: "Services",
        link: "services"
    },
    {
        title: "Contact",
        link: "contact"
    }
]
about 4 years ago · Juan Pablo Isaza
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!