You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
899 B
36 lines
899 B
7 years ago
|
class ScrollingList extends React.Component {
|
||
|
listRef = null;
|
||
|
|
||
|
// highlight-range{1-8}
|
||
|
getSnapshotBeforeUpdate(prevProps, prevState) {
|
||
|
// Are we adding new items to the list?
|
||
|
// Capture the current height of the list so we can adjust scroll later.
|
||
|
if (prevProps.list.length < this.props.list.length) {
|
||
|
return this.listRef.scrollHeight;
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
// highlight-range{1-8}
|
||
|
componentDidUpdate(prevProps, prevState, snapshot) {
|
||
|
// If we have a snapshot value, we've just added new items.
|
||
|
// Adjust scroll so these new items don't push the old ones out of view.
|
||
|
if (snapshot !== null) {
|
||
|
this.listRef.scrollTop +=
|
||
|
this.listRef.scrollHeight - snapshot;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
render() {
|
||
|
return (
|
||
|
<div ref={this.setListRef}>
|
||
|
{/* ...contents... */}
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
setListRef = ref => {
|
||
|
this.listRef = ref;
|
||
|
};
|
||
|
}
|