I have a question about React functional components, specifically about functions in functional components. For instance:
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 ()
}
This is just a quick demo on how to use useEffect
to subscribe to event, useRef
to create element ref for event listener, and useState
to store the event results.
Please note that this is for the sake of demonstration purpose only. Calling setState
in every tick of scroll event callback is NOT ideal.
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);
Here is a working demo in code sandbox: https://codesandbox.io/s/react-functional-component-event-listener-demo-fmerz?fontsize=14
You should add and remove the eventlistener in the same useEffect
-call. For instance:
import React, { useEffect } from 'react';
const Component = (props) => {
useEffect(() => {
function handleScroll() {
const scrollTop = window.scrollY;
console.log(scrollTop);
}
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, []);
return (
<div />
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With