Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lekaha/5bc81dadbf2aac88dded6914203fc2da to your computer and use it in GitHub Desktop.
Save lekaha/5bc81dadbf2aac88dded6914203fc2da to your computer and use it in GitHub Desktop.
Setting scroll position with ReactJS
// 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