Search code examples
javascriptreact-nativelifecyclereact-lifecycle

Lifecycle Management in React-native


What is the life cycle when exiting an app with Swipe in Ios Simulator? I tried to log every lifecycle, but nothing happened. I want to execute a specific function when the app exits, but I'm not sure what to do. Has anyone had a similar problem with me?


Solution

  • There's no way to detect if the app is closing, but you can check if the app is in background using AppState.

    In your parent component, add:

    import { AppState } from 'react-native';
    
    ...
    
    componentDidMount() {
        AppState.addEventListener('change', this._handleAppStateChange);
    }
    
    ...
    
    _handleAppStateChange = (nextAppState) => {
        //based of nextAppState, do what you need
    }
    

    Source: https://facebook.github.io/react-native/docs/appstate