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

301
Views
ReactJS, onScroll doesn't work for dynamic navbar

I'm trying to make a navbar visible only when the user passes the header section. I set up a simple function to be triggered by "onScroll". However it's not really working.

Component:

function App() {
  const [productArray, setProductArray] = useState([]);
  const [navBarClass, setNavBarClass] = useState("hidden");

  const navBarScroll = () => {
    setNavBarClass("visible");
  }; // this should make the navbar visible

  return (
    <div className="App">
      <Header onScroll={navBarScroll} /> // Here's the onScroll
      <ProductContainer
        productArray={productArray}
        setProductArray={setProductArray}
      />
      <About />
      <Footer />
      <NavBar className={navBarClass} /> // This is the nav bar 
    </div>
  );
}

Header Component:

const Header = () => {
  return (
    <div className="header" id="home">
      <div className="header-sub">
        <div className="div-1">
          <img src={logo} alt="logo" className="logo" />
          <div className="menu">
            <a href="#home">Home</a>
            <a href="#about">About</a>
            <a href="#shop">Shop</a>
            <a href="mailto:example@gmail.com">Contact</a>
            <ShoppingCartIcon className="btn" />

            <p></p>
          </div>
        </div>
        <div className="div-2">
          <FadeIn transitionDuration="2500">
            <h1>AS UNIQUE AS YOU ARE.</h1>
          </FadeIn>
        </div>
        <div className="div-3"></div>
      </div>
    </div>
  );
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Issue is onScroll is not being consumed in Header component. We need to add onScroll event on HTML tag in order for it to be invoked.

You can read onScroll prop in Header component and pass it to parent div as below and it should get called:

const Header = ({onScroll}) => {
  return (
    <div className="header" id="home" onScroll={onScroll}>
      <div className="header-sub">
        <div className="div-1">
          <img src={logo} alt="logo" className="logo" />
          <div className="menu">
            <a href="#home">Home</a>
            <a href="#about">About</a>
            <a href="#shop">Shop</a>
            <a href="mailto:example@gmail.com">Contact</a>
            <ShoppingCartIcon className="btn" />

            <p></p>
          </div>
        </div>
        <div className="div-2">
          <FadeIn transitionDuration="2500">
            <h1>AS UNIQUE AS YOU ARE.</h1>
          </FadeIn>
        </div>
        <div className="div-3"></div>
      </div>
    </div>
  );
};
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!