From ff2e865265948b9e76cb2d0fb15c22831abb038f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20D=C3=ADaz?= Date: Wed, 27 Mar 2019 13:48:17 +0100 Subject: [PATCH 1/2] Prevent React setState on unmounted Component --- index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8528ba2..9c896f3 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ import React, { Component } from 'react' import { View, NativeMethodsMixin, Dimensions } from 'react-native' exports.InViewPort = class extends Component { + isMounted = true constructor(props) { super(props) this.state = { rectTop: 0, rectBottom: 0 } @@ -16,6 +17,7 @@ exports.InViewPort = class extends Component { } componentWillUnmount() { + this.isMounted = false this.stopWatching() } @@ -37,11 +39,13 @@ exports.InViewPort = class extends Component { return } this.myview.measure((x, y, width, height, pageX, pageY) => { - this.setState({ - rectTop: pageY, - rectBottom: pageY + height, - rectWidth: pageX + width - }) + if (this.isMounted) { + this.setState({ + rectTop: pageY, + rectBottom: pageY + height, + rectWidth: pageX + width + }) + } }) this.isInViewPort() }, this.props.delay || 100) From debc4cab3770814bec2fb8ac4325afdee0024155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20D=C3=ADaz?= Date: Thu, 28 Mar 2019 13:30:48 +0100 Subject: [PATCH 2/2] isMounted "flag" set to true inside componentDidMount() method --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9c896f3..18ccf8c 100644 --- a/index.js +++ b/index.js @@ -4,13 +4,14 @@ import React, { Component } from 'react' import { View, NativeMethodsMixin, Dimensions } from 'react-native' exports.InViewPort = class extends Component { - isMounted = true + isMounted = false constructor(props) { super(props) this.state = { rectTop: 0, rectBottom: 0 } } componentDidMount() { + this.isMounted = true if (!this.props.disabled) { this.startWatching() }