From 93d7697dcc3acea4bc9df8090e122e296c110819 Mon Sep 17 00:00:00 2001 From: enahum Date: Mon, 17 Jul 2017 12:26:53 -0400 Subject: [PATCH] RN-261 Fix app crashing when badge is unmounted before it displays (#743) --- app/components/badge.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/components/badge.js b/app/components/badge.js index 1d08db4b9..c5a0ab008 100644 --- a/app/components/badge.js +++ b/app/components/badge.js @@ -33,6 +33,7 @@ export default class Badge extends PureComponent { super(props); this.width = 0; + this.mounted = false; } componentWillMount() { @@ -45,12 +46,26 @@ export default class Badge extends PureComponent { }); } + componentDidMount() { + this.mounted = true; + } + + componentWillUnmount() { + this.mounted = false; + } + handlePress = () => { if (this.props.onPress) { this.props.onPress(); } }; + setNativeProps = (props) => { + if (this.mounted && this.refs.badgeContainer) { + this.refs.badgeContainer.setNativeProps(props); + } + }; + onLayout = (e) => { let width; @@ -66,7 +81,7 @@ export default class Badge extends PureComponent { this.width = width; const height = Math.max(e.nativeEvent.layout.height, this.props.minHeight); const borderRadius = height / 2; - this.refs.badgeContainer.setNativeProps({ + this.setNativeProps({ style: { width, height, @@ -74,7 +89,7 @@ export default class Badge extends PureComponent { } }); setTimeout(() => { - this.refs.badgeContainer.setNativeProps({ + this.setNativeProps({ style: { display: 'flex' }