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

215
Views
how to detect the scroll reaches the specific component in react js using ref

how can we identify onScoll event reaches a specific component? I have tried to use ref to scrollinto view thats works when we fire click event. But how it works user scroll down and reaches specific component, that time i should perform some task. how can we achive that?

 import "./styles.css";
import { useRef, useEffect } from "react";
export default function App() {
  const eipCard = useRef(null);
  const salaryCard = useRef(null);
  const salesCard = useRef(null);

  const handleStickyHeader = () => {
     //handle logic

    // should trigger scroll reaches eip card
    console.log("reaches eip card")
    // should trigger scroll reaches salary card
    console.log("reaches salary card")
    // should trigger scroll reaches sales card
    console.log("reaches sales card")
  };

  useEffect(() => {
    if (typeof window !== "undefined") {
      window.addEventListener("scroll", handleStickyHeader);
    }
    return () => window.removeEventListener("scroll", handleStickyHeader);
  }, []);

  return (
    <div className="App">
      <div
        ref={eipCard}
        style={{ height: "500px", backgroundColor: "red" }}
      ></div>
      <div
        ref={salaryCard}
        style={{ height: "500px", backgroundColor: "blue" }}
      ></div>
      <div
        ref={salesCard}
        style={{ height: "500px", backgroundColor: "blue" }}
      ></div>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use Intersection Observer API to create custom React hooks checking components in view. Here is the working Example

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!