Skip to content

Commit

Permalink
RNTesterIntegrationTests can fail due to the warning "Can't call setS…
Browse files Browse the repository at this point in the history
…tate (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
  • Loading branch information
tom-un authored and facebook-github-bot committed May 22, 2019
1 parent 592e6c5 commit 6671165
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions RNTester/js/RNTesterApp.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,19 @@ const Header = ({onBack, title}: {onBack?: () => mixed, title: string}) => (
);

class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
_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,
);
Expand All @@ -83,6 +89,10 @@ class RNTesterApp extends React.Component<Props, RNTesterNavigationState> {
});
}

componentWillUnmount() {
this._mounted = false;
}

_handleBack = () => {
this._handleAction(RNTesterActions.Back());
};
Expand Down

0 comments on commit 6671165

Please sign in to comment.