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 :: onclick string 
Javascript :: javascript check collision 
Javascript :: jquery ajax endpoint 
Javascript :: How to know react and react-native version 
Javascript :: query selector by href 
Javascript :: module build failed (from ./node_modules/css-loader/dist/cjs.js): 
Javascript :: npm package for sorting in reactjs 
Javascript :: moment get month name 
Javascript :: react native flatlist from bottom to top 
Javascript :: asyncstorage react native 
Javascript :: reactjs app change port 
Javascript :: get text inside span vue test utils 
Javascript :: react confirm alert 
Javascript :: redirect using javascript 
Javascript :: redirect through javascript 
Javascript :: vehicle number formik validation 
Javascript :: javascript MAX INT and MIN INT 
Javascript :: hover jquery 
Javascript :: disemvowel trolls codewars javascript 
Javascript :: react next alias import 
Javascript :: jquery load event 
Javascript :: get keys wher value is true in object in javascript 
Javascript :: js make value positive 
Javascript :: falsy values in array 
Javascript :: mouseover javascript 
Javascript :: jquery ajax while loading 
Javascript :: between in mongodb 
Javascript :: foreach object js 
Javascript :: insertbefore jquery 
Javascript :: javascript is valid json string 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =