From 6671165f69e37a49af8b709b4807f9049f7606c3 Mon Sep 17 00:00:00 2001 From: Tom Underhill Date: Tue, 21 May 2019 23:17:49 -0700 Subject: [PATCH] RNTesterIntegrationTests can fail due to the warning "Can't call setState (or forceUpdate) on an unmounted component." (#24984) Summary: When running the RNTesterIntegrationTests from the XCode IDE or xcodebuild command line, its possible for tests to intermittently fail due to the warning `'Warning: Can\'t call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.%s', '\n in RNTesterApp.` . A setState() call could happen in the AsyncStorage callback after the component had unmounted: presumably because the test ran and concluded before AsyncStorage returned. Changed RNTesterApp.ios.js to maintain a _mounted field that is set and cleared in componentDidMount() and componentWillUnmount() and refrain from calling setState() if the flag is false. ## Changelog [iOS] [Fixed] - Fixed RNTesterApp to not call setState() after the component has been unmounted. Pull Request resolved: https://github.com/facebook/react-native/pull/24984 Differential Revision: D15447898 Pulled By: hramos fbshipit-source-id: b01bc0a40a4f4d548aca0a4bb891ba3f4c8d0612 --- RNTester/js/RNTesterApp.ios.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/RNTester/js/RNTesterApp.ios.js b/RNTester/js/RNTesterApp.ios.js index e13178d024e797..411e352bd23d06 100644 --- a/RNTester/js/RNTesterApp.ios.js +++ b/RNTester/js/RNTesterApp.ios.js @@ -61,13 +61,19 @@ const Header = ({onBack, title}: {onBack?: () => mixed, title: string}) => ( ); class RNTesterApp extends React.Component { + _mounted: boolean; + UNSAFE_componentWillMount() { BackHandler.addEventListener('hardwareBackPress', this._handleBack); } componentDidMount() { + this._mounted = true; Linking.getInitialURL().then(url => { AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => { + if (!this._mounted) { + return; + } const exampleAction = URIActionMap( this.props.exampleFromAppetizeParams, ); @@ -83,6 +89,10 @@ class RNTesterApp extends React.Component { }); } + componentWillUnmount() { + this._mounted = false; + } + _handleBack = () => { this._handleAction(RNTesterActions.Back()); };