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?

2
  • 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. Your crash will be hard to diagnose without a repro or stack trace - what's really happening in your cleanup? Commented Aug 6, 2023 at 2:48
  • I don't believe that's correct -- according to this, 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. Commented Aug 6, 2023 at 21:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.