Improve IntersectionObserverArticle perf (#5152)

This commit is contained in:
Nolan Lawson 2017-09-30 05:28:49 -07:00 committed by Eugen Rochko
parent 83ffc4dc07
commit ca0e8be20c
1 changed files with 21 additions and 16 deletions

View File

@ -58,28 +58,33 @@ export default class IntersectionObserverArticle extends React.Component {
} }
handleIntersection = (entry) => { handleIntersection = (entry) => {
const { onHeightChange, saveHeightKey, id } = this.props; this.entry = entry;
if (this.node && this.node.children.length !== 0) { scheduleIdleTask(this.calculateHeight);
// save the height of the fully-rendered element this.setState(this.updateStateAfterIntersection);
this.height = getRectFromEntry(entry).height; }
updateStateAfterIntersection = (prevState) => {
if (prevState.isIntersecting && !this.entry.isIntersecting) {
scheduleIdleTask(this.hideIfNotIntersecting);
}
return {
isIntersecting: this.entry.isIntersecting,
isHidden: false,
};
}
calculateHeight = () => {
const { onHeightChange, saveHeightKey, id } = this.props;
// save the height of the fully-rendered element (this is expensive
// on Chrome, where we need to fall back to getBoundingClientRect)
this.height = getRectFromEntry(this.entry).height;
if (onHeightChange && saveHeightKey) { if (onHeightChange && saveHeightKey) {
onHeightChange(saveHeightKey, id, this.height); onHeightChange(saveHeightKey, id, this.height);
} }
} }
this.setState((prevState) => {
if (prevState.isIntersecting && !entry.isIntersecting) {
scheduleIdleTask(this.hideIfNotIntersecting);
}
return {
isIntersecting: entry.isIntersecting,
isHidden: false,
};
});
}
hideIfNotIntersecting = () => { hideIfNotIntersecting = () => {
if (!this.componentMounted) { if (!this.componentMounted) {
return; return;