From a3593d45b7f2b6be76d85da578bc059bd7450187 Mon Sep 17 00:00:00 2001 From: Arfan Fudyartanto Date: Fri, 19 Oct 2018 15:03:23 +0700 Subject: [PATCH] Fix Warning: Can't call setState (or forceUpdate) on an unmounted component --- src/index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index a3e50db..745de59 100644 --- a/src/index.js +++ b/src/index.js @@ -34,6 +34,7 @@ export default class ContentLoader extends Component { ], frequence: props.duration / 2 } + this._isMounted = false; this._animate = new Animated.Value(0) this.loopAnimation = this .loopAnimation @@ -49,8 +50,12 @@ export default class ContentLoader extends Component { return x } componentDidMount(props) { + this._isMounted = true this.loopAnimation() } + componentWillUnmount() { + this._isMounted = false + } loopAnimation() { @@ -73,7 +78,9 @@ export default class ContentLoader extends Component { offsetValues[0] = this.offsetValueBound(newState.offsetValues[0]); offsetValues[1] = this.offsetValueBound(newState.offsetValues[1]); offsetValues[2] = this.offsetValueBound(newState.offsetValues[2]); - this.setState({offsets: offsetValues}); + if (this._isMounted) { + this.setState({offsets: offsetValues}); + } if (t < 1) { requestAnimationFrame(this._animation); }