Skip to content

Commit fbe71a7

Browse files
committed
Add a warning about component being rendered for listeners
1 parent 17108e2 commit fbe71a7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class Profile extends React.Component {
269269
}
270270
```
271271

272-
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-object.md#getparent) to get a reference to the parent screen's navigation object and add a listener.
272+
Keep in mind 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-object.md#getparent) to get a reference to the parent screen's navigation object and add a listener.
273273

274274
```js
275275
const unsubscribe = navigation
@@ -281,6 +281,12 @@ const unsubscribe = navigation
281281

282282
Here `'MyTabs'` refers to the value you pass in the `id` prop of the parent `Tab.Navigator` whose event you want to listen to.
283283

284+
:::warning
285+
286+
The component needs to be rendered for the listeners to be added in `useEffect` or `componentDidMount`. Navigators such as [bottom tabs](bottom-tab-navigator.md) and [drawer](drawer-navigator.md) lazily render the screen after navigating to it. So if your listener is not being called, double check that the component is rendered.
287+
288+
:::
289+
284290
### `listeners` prop on `Screen`
285291

286292
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.

0 commit comments

Comments
 (0)