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.
38 lines
1005 B
38 lines
1005 B
class ScrollingList extends React.Component {
|
|
listRef = null;
|
|
|
|
// highlight-range{1-10}
|
|
getSnapshotBeforeUpdate(prevProps, prevState) {
|
|
// Are we adding new items to the list?
|
|
// Capture the scroll position so we can adjust scroll later.
|
|
if (prevProps.list.length < this.props.list.length) {
|
|
return (
|
|
this.listRef.scrollHeight - this.listRef.scrollTop
|
|
);
|
|
}
|
|
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.
|
|
// (snapshot here is the value returned from getSnapshotBeforeUpdate)
|
|
if (snapshot !== null) {
|
|
this.listRef.scrollTop =
|
|
this.listRef.scrollHeight - snapshot;
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<div ref={this.setListRef}>
|
|
{/* ...contents... */}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
setListRef = ref => {
|
|
this.listRef = ref;
|
|
};
|
|
}
|
|
|