From e725d302c28d012fcd69f2b1564543039a47af08 Mon Sep 17 00:00:00 2001 From: Lior Brauer Date: Mon, 29 May 2017 22:10:37 +0300 Subject: [PATCH 1/2] Prevent calling setState on unmounted WindowScroller component This a bit of an edge case, when rendering several WindowScroller components on the same page inside a virtual list, the scroll listener can sometimes call setState on an unmounted WindowScroller component (that been scrolled out of view in the virtual list), triggering react's warning. This PR prevents calling setState if the component has been unmounted. --- source/WindowScroller/WindowScroller.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/WindowScroller/WindowScroller.js b/source/WindowScroller/WindowScroller.js index 5962dab79..8d5daf165 100644 --- a/source/WindowScroller/WindowScroller.js +++ b/source/WindowScroller/WindowScroller.js @@ -88,6 +88,8 @@ export default class WindowScroller extends PureComponent { registerScrollListener(this, scrollElement) window.addEventListener('resize', this._onResize, false) + + this._isMounted = true } componentWillReceiveProps (nextProps) { @@ -106,6 +108,8 @@ export default class WindowScroller extends PureComponent { unregisterScrollListener(this, this.props.scrollElement || window) window.removeEventListener('resize', this._onResize, false) + + this._isMounted = false } render () { @@ -127,6 +131,8 @@ export default class WindowScroller extends PureComponent { // Referenced by utils/onScroll __handleWindowScrollEvent (event) { + if (!this._isMounted) return; + const { onScroll } = this.props const scrollElement = this.props.scrollElement || window From cbbacc24de6e3df2acaec909f4e81862b4b9febe Mon Sep 17 00:00:00 2001 From: Lior Brauer Date: Mon, 29 May 2017 22:23:16 +0300 Subject: [PATCH 2/2] fix code style --- source/WindowScroller/WindowScroller.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/WindowScroller/WindowScroller.js b/source/WindowScroller/WindowScroller.js index 8d5daf165..425ed9099 100644 --- a/source/WindowScroller/WindowScroller.js +++ b/source/WindowScroller/WindowScroller.js @@ -88,7 +88,7 @@ export default class WindowScroller extends PureComponent { registerScrollListener(this, scrollElement) window.addEventListener('resize', this._onResize, false) - + this._isMounted = true } @@ -108,7 +108,7 @@ export default class WindowScroller extends PureComponent { unregisterScrollListener(this, this.props.scrollElement || window) window.removeEventListener('resize', this._onResize, false) - + this._isMounted = false } @@ -131,8 +131,8 @@ export default class WindowScroller extends PureComponent { // Referenced by utils/onScroll __handleWindowScrollEvent (event) { - if (!this._isMounted) return; - + if (!this._isMounted) return + const { onScroll } = this.props const scrollElement = this.props.scrollElement || window