componentWillUnmount doesn't work in Expo Client app?

Please provide the following:

  1. SDK Version: 38.0.8
  2. Platforms(Android/iOS/web/all): Android and iOS

Hi,
I’m currently testing through the Expo app. I’m trying to detect when the app is backgrounded/killed from the multitasking app tray.

    componentDidMount() {
        console.log("HomeMainScreen | ComponentDidMount -----------------");
        ...
    }

    componentWillUnmount() {
        console.log("HomeMainScreen | componentWillUnmount -----------------", this.props);
        ...
    }

I can see the componentDidMount() working. But when exiting the Expo app, the “componentWillUnmount” doesn’t get called. I’m wondering if this is due to me testing it in through the Expo app but would work when published to the AppStore/Play Store, or if it’s just not working properly.

Thanks

Hello @anthonyli, these methods do not work this way.

componentDidMount() is invoked immediately after a component is mounted (inserted into the tree). this mean will be triggered when react render this component.

otherwise componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. See that is when The Component will be destroyed, not when the app will be destroyed.

This “events” are not about de app. Anyway, if you want to have a listener to app background state you can use something like:

import {AppState, Text} from 'react-native'
...
componentDidMount() {
    AppState.addEventListener('change', this.YourListenner);
}
componentWillUnmount() {
    AppState.removeEventListener('change', this.YourListenner);
}

See more:

1 Like

Thanks!
I followed the SO post and it works for the Expo app

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.