In the previous section, we worked with a stack navigator that has two screens (
Details) and learned how to use
this.props.navigation.navigate('RouteName') to navigate between the routes.
An important question in this context is: what happens with
Home when we navigate away from it, or when we come back to it? How does a route find out that a user is leaving it or coming back to it?
Coming to React Navigation from the web, you may assume that when user navigates from route
A to route
A will unmount and its
componentWillUnmount will be called. You may also expect
A will remount again when user comes back to it. While these React lifecycle methods are still valid and are used in React Navigation, their usage differs from the web. This is driven by more complex needs of mobile navigation.
Consider a stack navigator with screens
B. After navigating to
componentDidMount is called. When pushing
componentDidMount is also called, but
A remains mounted on the stack and its
componentWillUnmount is therefore not called.
When going back from
B is called, but
A is not because
A remained mounted the whole time.
Subscribing to lifecycle events
Now that we understand how React lifecycle methods work in React Navigation, let's answer the question we asked at the beginning: "How do we find out that a user is leaving it or coming back to it?"
React Navigation emits events to screen components that subscribe to them. There are four different events that you can subscribe to:
didBlur. Read more about them in the API reference.
- While Reacts lifecycle methods are still valid, React Navigation adds more lifecycle events that you can subscribe to through the
- You may also use the
<NavigationEvents />component to react to lifecycle changes.