Skip to content

Commit 9d64d38

Browse files
committed
Emphasize ID prop for getParent
1 parent 5644527 commit 9d64d38

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

versioned_docs/version-5.x/navigation-events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ Each callback registered as an event listener receive an event object as its arg
2121
- `target` - The route key for the screen that should receive the event. For some events, this maybe `undefined` if the event wasn't related to a specific screen.
2222
- `preventDefault` - For some events, there may be a `preventDefault` method on the event object. Calling this method will prevent the default action performed by the event (such as switching tabs on `tabPress`). Support for preventing actions are only available for certain events like `tabPress` and won't work for all events.
2323

24-
One thing to keep in mind is that you can only listen to events from the immediate parent navigator. For example, if you try to add a listener in a screen is inside a stack that's nested in a tab, it won't get the `tabPress` event. If you need to listen to an event from a parent navigator, you may use `navigation.getParent()` to get a reference to parent navigator's navigation prop and add a listener.
25-
2624
There are 2 ways to listen to events:
2725

2826
### `navigation.addListener`
@@ -78,6 +76,8 @@ class Profile extends React.Component {
7876
}
7977
```
8078

79+
One thing to keep in mind is that you can only listen to events from the immediate navigator with `addListener`. For example, if you try to add a listener in a screen that's inside a stack that's nested in a tab, it won't get the `tabPress` event. If you need to listen to an event from a parent navigator, you may use `navigation.getParent()` to get a reference to parent navigator's navigation prop and add a listener.
80+
8181
### `listeners` prop on `Screen`
8282

8383
Sometimes you might want to add a listener from the component where you defined the navigator rather than inside the screen. You can use the `listeners` prop on the `Screen` component to add listeners. The `listeners` prop takes an object with the event names as keys and the listener callbacks as values.

versioned_docs/version-6.x/navigation-events.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ This event is emitted when the user is leaving the screen, there's a chance to [
3232

3333
## Listening to events
3434

35-
There are multiple ways to listen to events from the navigators. Before we get into how to add listeners, one thing to keep in mind is that you can only listen to events from the immediate parent navigator. For example, if you try to add a listener in a screen is inside a stack that's nested in a tab, it won't get the `tabPress` event. If you need to listen to an event from a parent navigator, you may use [`navigation.getParent`](navigation-prop.md#getparent) to get a reference to parent navigator's navigation prop and add a listener.
36-
37-
Each callback registered as an event listener receive an event object as its argument. The event object contains few properties:
35+
There are multiple ways to listen to events from the navigators. Each callback registered as an event listener receives an event object as its argument. The event object contains few properties:
3836

3937
- `data` - Additional data regarding the event passed by the navigator. This can be `undefined` if no data was passed.
4038
- `target` - The route key for the screen that should receive the event. For some events, this maybe `undefined` if the event wasn't related to a specific screen.
@@ -95,6 +93,18 @@ class Profile extends React.Component {
9593
}
9694
```
9795

96+
One thing to keep in mind is that you can only listen to events from the immediate navigator with `addListener`. For example, if you try to add a listener in a screen that's inside a stack that's nested in a tab, it won't get the `tabPress` event. If you need to listen to an event from a parent navigator, you may use [`navigation.getParent`](navigation-prop.md#getparent) to get a reference to parent navigator's navigation prop and add a listener.
97+
98+
```js
99+
const unsubscribe = navigation
100+
.getParent('MyTabs')
101+
.addListener('tabPress', (e) => {
102+
// Do something
103+
});
104+
```
105+
106+
Here `'MyTabs'` refers to the value you pass in the `id` prop of the parent `Tab.Navigator` whose event you want to listen to.
107+
98108
### `listeners` prop on `Screen`
99109

100110
Sometimes you might want to add a listener from the component where you defined the navigator rather than inside the screen. You can use the `listeners` prop on the `Screen` component to add listeners. The `listeners` prop takes an object with the event names as keys and the listener callbacks as values.
@@ -136,7 +146,7 @@ Example:
136146

137147
### `screenListeners` prop on the navigator
138148

139-
You can pass a prop named `screenListeners` to the navigator component, where you can specify listeners for events from all screens. This can be useful if you want to listen to specific events regardless of the screen, or want to listen to common events such as `state` which is emitted to all screens.
149+
You can pass a prop named `screenListeners` to the navigator component, where you can specify listeners for events from all screens for this navigator. This can be useful if you want to listen to specific events regardless of the screen, or want to listen to common events such as `state` which is emitted to all screens.
140150

141151
Example:
142152

versioned_docs/version-6.x/navigation-prop.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ To use an ID for a navigator, first pass a unique `id` prop:
358358
Then when using `getParent`, instead of:
359359

360360
```js
361+
// Avoid this
361362
const drawerNavigation = navigation.getParent().getParent();
362363

363364
// ...
@@ -368,6 +369,7 @@ drawerNavigation?.openDrawer();
368369
You can do:
369370
370371
```js
372+
// Do this
371373
const drawerNavigation = navigation.getParent('LeftDrawer');
372374

373375
// ...

versioned_docs/version-6.x/nesting-navigators.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,15 @@ For example, if you have a stack navigator nested inside a tab navigator, the sc
8787
To receive events from parent navigator, you can explicitly listen to parent's events with [`navigation.getParent`](navigation-prop.md#getparent):
8888

8989
```js
90-
const unsubscribe = navigation.getParent().addListener('tabPress', (e) => {
91-
// Do something
92-
});
90+
const unsubscribe = navigation
91+
.getParent('MyTabs')
92+
.addListener('tabPress', (e) => {
93+
// Do something
94+
});
9395
```
9496

97+
Here `'MyTabs'` refers to the value you pass in the `id` prop of the parent `Tab.Navigator` whose event you want to listen to.
98+
9599
### Parent navigator's UI is rendered on top of child navigator
96100

97101
For example, when you nest a stack navigator inside a drawer navigator, you'll see that the drawer appears above the stack navigator's header. However, if you nest the drawer navigator inside a stack, the drawer will appear below the header of the stack. This is an important point to consider when deciding how to nest your navigators.

versioned_docs/version-6.x/upgrading-from-5.x.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ React Navigation 6 requires newer versions of following libraries:
2222
To upgrade `react-native-safe-area-context` and `react-native-screens` to the latest supported versions, do the following:
2323

2424
> **NOTE:**
25-
>
25+
>
2626
> If your react-native Version is <= 0.63.4, don't use `react-native-safe-area-context` in Version 4, but only till 3.4.1. [More Information see here](https://github.com/th3rdwave/react-native-safe-area-context/issues/248)
2727
2828
For Expo managed projects:
@@ -177,7 +177,7 @@ A common scenario where you should use `merge: true` is if you have a custom tab
177177

178178
### Dropped `dangerously` from `dangerouslyGetParent` and `dangerouslyGetState`
179179

180-
The `dangerouslyGetParent` and `dangerouslyGetState` methods on the `navigation` prop are useful in many scenarios, and sometimes necessary. So we dropped the `dangerously` prefix to make it clear that it's safe to use. Now you can use [`navigation.getParent()`](navigation-prop.md#getparent) and [`navigation.getState()`](navigation-prop.md#getstate).
180+
The `dangerouslyGetParent` and `dangerouslyGetState` methods on the `navigation` prop are useful in many scenarios, and sometimes necessary. So we dropped the `dangerously` prefix to make it clear that it's safe to use. Now you can use [`navigation.getParent`](navigation-prop.md#getparent) and [`navigation.getState()`](navigation-prop.md#getstate).
181181

182182
### No more `state` property on the `route` prop
183183

0 commit comments

Comments
 (0)