Skip to content

Commit 35e30a8

Browse files
authored
document nav lifecycle (#251)
* document nav lifecycle * address review
1 parent e700d5a commit 35e30a8

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

docs/custom-android-back-button-handling.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,4 @@ The presented approach will work well for screens that are shown in a `StackNavi
5757

5858
### Why not use component lifecycle methods?
5959

60-
At first, you may be inclined to use `componentDidMount` to subscribe for the back press event and `componentWillUnmount` to unsubscribe. Reason why we do not use them is that they are not generally called when entering or leaving a screen.
61-
62-
More specifically, consider a `StackNavigator` with screens `A` and `B`. After navigating to `A`, its `componentDidMount` is called. When pushing `B`, its `componentDidMount` is also called, but `A` remains mounted and its `componentWillUnmount` is therefore not called.
63-
64-
Similarly, when going back from `B` to `A`, `componentWillUnmount` of `B` is called, but `componentDidMount` of `A` is not because `A` remained mounted the whole time.
60+
At first, you may be inclined to use `componentDidMount` to subscribe for the back press event and `componentWillUnmount` to unsubscribe. This approach will not work - learn more about this in [navigation lifecycle](navigation-lifecycle.html).

docs/navigation-lifecycle.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
id: navigation-lifecycle
3+
title: Navigation lifecycle
4+
sidebar_label: Navigation lifecycle
5+
---
6+
7+
In the previous section, we worked with a stack navigator that has two screens (`Home` and `Details`) and learned how to use `this.props.navigation.navigate('RouteName')` to navigate between the routes.
8+
9+
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?
10+
11+
Coming to react-navigation from the web, you may assume that when user navigates from route A to route B, A will unmount (its `componentWillUnmount` is called) and A will mount 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.
12+
13+
## Example scenario
14+
15+
Consider a stack navigator with screens A and B. After navigating to A, its `componentDidMount` is called. When pushing B, its `componentDidMount` is also called, but A remains mounted on the stack and its `componentWillUnmount` is therefore not called.
16+
17+
When going back from B to A, `componentWillUnmount` of B is called, but `componentDidMount` of A is not because A remained mounted the whole time.
18+
19+
Similar results can be observed (in combination) with other navigators as well. Consider a tab navigator with two tabs, where each tab is a stack navigator:
20+
21+
22+
```jsx
23+
const HomeStack = createStackNavigator({
24+
Home: HomeScreen,
25+
Details: DetailsScreen,
26+
});
27+
28+
const SettingsStack = createStackNavigator({
29+
Settings: SettingsScreen,
30+
Profile: ProfileScreen,
31+
});
32+
33+
const TabNavigator = createBottomTabNavigator(
34+
{
35+
Home: HomeStack,
36+
Settings: SettingsStack,
37+
}
38+
);
39+
```
40+
41+
We start on the `HomeScreen` and navigate to `DetailsScreen`. Then we use the tab bar to switch to the `SettingsScreen` and navigate to `ProfileScreen`. After this sequence of operations is done, all 4 of the screens are mounted! If you use the tab bar to switch back to the `HomeStack`, you'll notice you'll be presented with the `DetailsScreen` - the navigation state of the `HomeStack` has been preserved!
42+
43+
## React Navigation lifecycle events
44+
45+
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?"
46+
47+
React Navigation emits events to screen components that subscribe to them. There are four different events that you can subscribe to: `willFocus`, `willBlur`, `didFocus` and `didBlur`. Read more about them in the [API reference](navigation-prop.html#addlistener-subscribe-to-updates-to-navigation-lifecycle).
48+
49+
Many of your use cases may be covered with the [`withNavigationFocus` HOC](with-navigation-focus.html) or the [`<NavigationEvents />` component](navigation-events.html) which are a little more straightforward to use.
50+
51+
## Summary
52+
53+
- while React's lifecycle methods are still valid, React Navigation adds more lifecycle events that you can subscribe to through the `navigation` prop.
54+
- you may also use the `withNavigationFocus` HOC or `<NavigationEvents />` component to react to lifecycle changes

docs/navigation-prop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ Alternatively, as _screen A_ is the top of the stack, you can use `navigation.po
107107

108108
React Navigation emits events to screen components that subscribe to them:
109109

110-
* `willBlur` - the screen will be unfocused
111110
* `willFocus` - the screen will focus
111+
* `willBlur` - the screen will be unfocused
112112
* `didFocus` - the screen focused (if there was a transition, the transition completed)
113113
* `didBlur` - the screen unfocused (if there was a transition, the transition completed)
114114

website/i18n/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
"NavigationEvents": "NavigationEvents",
7575
"navigation-key": "Using the navigation key",
7676
"Using the navigation key": "Using the navigation key",
77+
"navigation-lifecycle": "Navigation lifecycle",
78+
"Navigation lifecycle": "Navigation lifecycle",
7779
"navigation-options-resolution": "Navigation options resolution",
7880
"Navigation options resolution": "Navigation options resolution",
7981
"navigation-prop": "Navigation prop reference",

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"getting-started",
55
"hello-react-navigation",
66
"navigating",
7+
"navigation-lifecycle",
78
"params",
89
"headers",
910
"header-buttons",

0 commit comments

Comments
 (0)