Skip to content

Commit 6671165

Browse files
tom-unfacebook-github-bot
authored andcommitted
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: #24984 Differential Revision: D15447898 Pulled By: hramos fbshipit-source-id: b01bc0a40a4f4d548aca0a4bb891ba3f4c8d0612
1 parent 592e6c5 commit 6671165

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

RNTester/js/RNTesterApp.ios.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ const Header = ({onBack, title}: {onBack?: () => mixed, title: string}) => (
6161
);
6262

6363
class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
64+
_mounted: boolean;
65+
6466
UNSAFE_componentWillMount() {
6567
BackHandler.addEventListener('hardwareBackPress', this._handleBack);
6668
}
6769

6870
componentDidMount() {
71+
this._mounted = true;
6972
Linking.getInitialURL().then(url => {
7073
AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => {
74+
if (!this._mounted) {
75+
return;
76+
}
7177
const exampleAction = URIActionMap(
7278
this.props.exampleFromAppetizeParams,
7379
);
@@ -83,6 +89,10 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
8389
});
8490
}
8591

92+
componentWillUnmount() {
93+
this._mounted = false;
94+
}
95+
8696
_handleBack = () => {
8797
this._handleAction(RNTesterActions.Back());
8898
};

0 commit comments

Comments
 (0)