How do I use pointer-events to react only to scroll events or is there a better approach

0 votes
can you tell me with the help of an exmaple How do I use pointer-events to react only to scroll events, or is there a better approach?
Feb 22 in Node-js by Nidhi
• 11,580 points
93 views

1 answer to this question.

0 votes

Capturing Only Scroll Events in React

If you need to listen only to scroll events while ignoring clicks and other interactions, use React’s onScroll event.

import { useEffect, useRef } from "react";

const ScrollOnlyComponent = () => {

  const scrollRef = useRef<HTMLDivElement>(null);

  useEffect(() => {

    const handleScroll = () => console.log("Scrolling...");

    const element = scrollRef.current;

    if (element) {

      element.addEventListener("scroll", handleScroll);

    }

    return () => {

      if (element) {

        element.removeEventListener("scroll", handleScroll);

      }

    };

  }, []);

  return (

    <div

      ref={scrollRef}

      style={{ overflowY: "scroll", height: "200px", background: "#eee" }}

    >

      <p style={{ height: "500px" }}>Scroll inside this box</p>

    </div>

  );

};

export default ScrollOnlyComponent;

answered Feb 22 by Kavya

Related Questions In Node-js

0 votes
1 answer
0 votes
0 answers
0 votes
1 answer

How do I redirect to a previous page in react?

You can use the useNavigate hook from ...READ MORE

answered Dec 31, 2024 in Node-js by Navya
82 views
0 votes
1 answer

What is the difference between React Synthetic Events and Native JavaScript Events, or how do they compare?

Feature React Synthetic Events (SyntheticEvent) Native JavaScript Events (Event) Definition React’s ...READ MORE

answered Feb 22 in Node-js by Kavya
55 views
0 votes
1 answer

How can I solve the issue of an uncontrolled input becoming controlled?

When working with form inputs in React, ...READ MORE

answered Nov 4, 2024 in Web Development by kavya
208 views
0 votes
1 answer

How to dynamically change meta tags before the site is scraped in Angular 2?

To dynamically change meta tags before Angular ...READ MORE

answered Nov 6, 2024 in Web Development by kavya
333 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP