Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

event listener for functional component

import React, { useEffect } from 'react';

const Component = (props) => {  
  useEffect(() => {
    window.addEventListener('scroll', handleScroll);
  });
  useEffect(() => {
    return () => {
      window.removeEventListener('scroll', handleScroll);
    };
  }, []);

  function handleScroll() {
    let scrollTop = window.scrollY;
  }


  return ()
}
Comment

give div event listener functional component

import React, { useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

const App = () => {
  // set default value
  const [scrollTop, setScrollTop] = useState(document.body.scrollTop);

  // create element ref
  const innerRef = useRef(null);

  useEffect(() => {
    const div = innerRef.current;
    // subscribe event
    div.addEventListener("scroll", handleOnScroll);
    return () => {
      // unsubscribe event
      div.removeEventListener("scroll", handleOnScroll);
    };
  }, []);

  const handleOnScroll = (e) => {
    // NOTE: This is for the sake of demonstration purpose only.
    // Doing this will greatly affect performance.
    setScrollTop(e.target.scrollTop);
  }

  return (
    <>
      {`ScrollTop: ${scrollTop}`}
      <div
        style={{
          overflow: 'auto',
          width: 500,
          height: 500,
          border: '1px solid black',
        }}
        ref={innerRef}
      >
        <div style={{ height: 1500, width: 1500 }}>
          Scroll Me
        </div>
      </div>
    </>
  )
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to flatten array with reduce in javascript 
Javascript :: javascript prompt for download location 
Javascript :: can immigrants vote in uk 
Javascript :: Codewars Convert a String to a Number! 
Javascript :: comprimento string javascript 
Javascript :: regex diferent of 
Javascript :: how to change the query parameter of the url in javascript 
Javascript :: js watch window resize 
Javascript :: how to disable mouse right click in html page 
Javascript :: how to create an invite discord.js 
Javascript :: react native text area align top 
Javascript :: update tooltip jquery 
Javascript :: js ask before close 
Javascript :: Make Array Consecutive javascript 
Javascript :: angular usehash not working 
Javascript :: regex para telefone celular 
Javascript :: new fabric canvas set width 
Javascript :: javascript hasclass 
Javascript :: js append query string to url 
Javascript :: express static auth 
Javascript :: npx: Create react chrome extension 
Javascript :: react-native link to play store 
Javascript :: js scrollto 
Javascript :: how to get the min value of two variables in math 
Javascript :: req.body empty mongodb 
Javascript :: Adblock detection in website using javascript 
Javascript :: vue.js cdn 
Javascript :: javascript log Time from Date 
Javascript :: mongoose.connect 
Javascript :: React Native Expo Scrollview Scroll to bottom 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =