From 9354ea705b4762e261cd7ff724eaf3bdd92d735e Mon Sep 17 00:00:00 2001 From: Yoad Snapir Date: Wed, 14 Nov 2018 19:00:31 +0200 Subject: [PATCH] call setState within requestAnimationFrame to prevent inifinite loop Fixes #104 --- src/with-content-rect.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/with-content-rect.js b/src/with-content-rect.js index d599e7e..ac0525f 100644 --- a/src/with-content-rect.js +++ b/src/with-content-rect.js @@ -30,6 +30,7 @@ function withContentRect(types) { componentWillMount() { this._resizeObserver = new ResizeObserver(this.measure) + this.animationFrameID = null; } componentWillUnmount() { @@ -37,6 +38,7 @@ function withContentRect(types) { this._resizeObserver.disconnect(this._node); } this._resizeObserver = null; + window.cancelAnimationFrame(this.animationFrameID); } measure = entries => { @@ -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) }