1

Given the following simplified app in react native:


    import { AppRegistry } from 'react-native';
    
    const App = () => {
      useEffect(() => {
        console.log('App mounted');
    
        return () => {
          console.log('App unmounted');
        };
      }, []);
    
      <View>
        <Text>Hello world</Text>
      </View>;
    };
    
    AppRegistry.registerComponent(appName, () => App);

Under what circumstances would App be unmounted, and the return function from the useEffect be called?

My assumption was "never" (it's either backgrounded, crashed or killed), but that doesn't seem to be the case. We have a very small minority of users where app is sometimes closing, shortly after being background, and the disposer on the useEffect is being called. And it only seems to be happening on Android.

Any clues?

lambinator
  • 10,616
  • 7
  • 55
  • 57
  • common misconception is that the cleanup function only runs when the component unmounts. In fact, they run on every re-render of the component. See [link](https://stackoverflow.com/questions/57023074/why-is-the-cleanup-function-from-useeffect-called-on-every-render). Your crash will be hard to diagnose without a repro or stack trace - what's really happening in your cleanup? – Abe Aug 06 '23 at 02:48
  • I don't believe that's correct -- according to [this](https://legacy.reactjs.org/docs/hooks-effect.html#explanation-why-effects-run-on-each-update), the cleanup function only runs on unmount due to the dependencies argument being an empty array ([]). To answer your other question -- there's no error or crash, just a shutdown. – lambinator Aug 06 '23 at 21:10

0 Answers0