Search code examples
react-nativereact-navigationlifecycle

Which life cycle method will be called when returning from another page in react native?


I have a page A, then navigate to page B, then go back. I want to know which life cycle method will be called on A, or event will be emitted?


Solution

  • When using react-navigation, you can subscribe to the didFocus and didBlur events to be notified when the user navigates to or from a screen.

    You can set these up in your component's componentDidMount using the addListener method of this.props.navigation like this:

    componentDidMount() {
        this.props.navigation.addListener("didFocus", () => {
            // user has navigated to this screen
        });
    
        this.props.navigation.addListener("didBlur", () => {
            // user has navigated away from this screen
        });
    }