Forked from ydeshayes/Setting scroll position with ReactJS
Created
August 29, 2018 00:56
-
-
Save fodra/0c73b5ac69560d24c6461b41d5c2a10c to your computer and use it in GitHub Desktop.
Setting scroll position with ReactJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Edit from http://blog.vjeux.com/2013/javascript/scroll-position-with-react.html | |
componentDidMount() { | |
const node = ReactDOM.findDOMNode(this); | |
node.scrollTop = node.scrollHeight; | |
} | |
componentWillUpdate: function() { | |
const node = this.getDOMNode(); | |
this.shouldScrollBottom = node.scrollTop + node.offsetHeight === node.scrollHeight; | |
}, | |
componentDidUpdate(prevProps) { | |
if (this.shouldScrollBottom) { | |
const node = ReactDOM.findDOMNode(this); | |
node.scrollTop = node.scrollHeight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment