I'm trying to make a 'scroll-up' button to appear and disappear depending on a scrolled position. So I'm trying to grab the scrolled position value dynamically but I am only able to grab the data once upon load, I want to grab it everytime i scroll.
I kind of know how to make it work with react-hooks with useEffect however the project is built using class components.
My approach to this was using componentDidMount like so:
componentDidMount() {
console.log('mount success')
console.log('pageYOffset', window.pageYOffset)
window.addEventListener('scroll', this.scrollHandler)
}
scrollHandler = () => {
const position = window.pageYOffset;
console.log('Current Scrolled', position)
}
The result in the console is always '0' since it loads first, and then as i scroll i dont get new feedback in the console.
Thanks in advance!