To build on @Michelle Tilley's answer, I sometimes want to scroll if the user's selection changes, so I trigger the scroll on componentDidUpdate
. I also did some math to figure out how far to scroll and whether scrolling was needed, which for me looks like the following:
componentDidUpdate() {_x000D_
let panel, node;_x000D_
if (this.refs.selectedSection && this.refs.selectedItem) {_x000D_
// This is the container you want to scroll. _x000D_
panel = this.refs.listPanel;_x000D_
// This is the element you want to make visible w/i the container_x000D_
// Note: You can nest refs here if you want an item w/i the selected item _x000D_
node = ReactDOM.findDOMNode(this.refs.selectedItem);_x000D_
}_x000D_
_x000D_
if (panel && node &&_x000D_
(node.offsetTop > panel.scrollTop + panel.offsetHeight || node.offsetTop < panel.scrollTop)) {_x000D_
panel.scrollTop = node.offsetTop - panel.offsetTop;_x000D_
}_x000D_
}
_x000D_