Wanto to get scroll position with Reactjs? You can use event listener in react like you will use in other js framework or library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | componentDidMount() { window.addEventListener('scroll', this.listenToScroll) } componentWillUnmount() { window.removeEventListener('scroll', this.listenToScroll) } listenToScroll = () => { const winScroll = document.body.scrollTop || document.documentElement.scrollTop const height = document.documentElement.scrollHeight - document.documentElement.clientHeight const scrolled = winScroll / height this.setState({ theposition: scrolled, }) } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.