Skip to content

Commit

Permalink
call setState within requestAnimationFrame to prevent inifinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
yoadsn committed Nov 14, 2018
1 parent eb34467 commit 9354ea7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/with-content-rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ function withContentRect(types) {

componentWillMount() {
this._resizeObserver = new ResizeObserver(this.measure)
this.animationFrameID = null;
}

componentWillUnmount() {
if (this._resizeObserver && this._node) {
this._resizeObserver.disconnect(this._node);
}
this._resizeObserver = null;
window.cancelAnimationFrame(this.animationFrameID);
}

measure = entries => {
Expand All @@ -49,8 +51,12 @@ function withContentRect(types) {
contentRect.entry = entries[0].contentRect
}

this.setState({ contentRect })

this.animationFrameID = window.requestAnimationFrame(() => {
if (this._resizeObserver) {
this.setState({ contentRect })
}
})

if (typeof this.props.onResize === 'function') {
this.props.onResize(contentRect)
}
Expand Down

0 comments on commit 9354ea7

Please sign in to comment.