Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer handleScroll in componentDidUpdate #265

Merged
merged 1 commit into from
Jul 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/waypoint.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export default class Waypoint extends React.Component {
}

// this._ref may occasionally not be set at this time. To help ensure that
// this works smoothly, we want to delay the initial execution until the
// next tick.
this.cancelInitialTimeout = onNextTick(() => {
// this works smoothly and to avoid layout thrashing, we want to delay the
// initial execution until the next tick.
this.cancelOnNextTick = onNextTick(() => {
this.cancelOnNextTick = null;

// Berofe doing anything, we want to check that this._ref is avaliable in Waypoint
ensureRefIsUsedByChild(this.props.children, this._ref);

Expand Down Expand Up @@ -87,8 +89,21 @@ export default class Waypoint extends React.Component {
return;
}

// The element may have moved.
this._handleScroll(null);
// The element may have moved, so we need to recompute its position on the
// page. This happens via handleScroll in a way that forces layout to be
// computed.
//
// We want this to be deferred to avoid forcing layout during render, which
// causes layout thrashing. And, if we already have this work enqueued, we
// can just wait for that to happen instead of enqueueing again.
if (this.cancelOnNextTick) {
return;
}

this.cancelOnNextTick = onNextTick(() => {
this.cancelOnNextTick = null;
this._handleScroll(null);
});
}

componentWillUnmount() {
Expand All @@ -103,8 +118,8 @@ export default class Waypoint extends React.Component {
this.resizeEventListenerUnsubscribe();
}

if (this.cancelInitialTimeout) {
this.cancelInitialTimeout();
if (this.cancelOnNextTick) {
this.cancelOnNextTick();
}
}

Expand Down