You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/navigation-events.md
+235-5Lines changed: 235 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,9 @@ title: Navigation events
4
4
sidebar_label: Navigation events
5
5
---
6
6
7
+
import Tabs from '@theme/Tabs';
8
+
import TabItem from '@theme/TabItem';
9
+
7
10
You can listen to various events emitted by React Navigation to get notified of certain events, and in some cases, override the default action. There are few core events such as `focus`, `blur` etc. (documented below) that work for every navigator, as well as navigator specific events that work only for certain navigators.
8
11
9
12
Apart from the core events, each navigator can emit their own custom events. For example, stack navigator emits `transitionStart` and `transitionEnd` events, tab navigator emits `tabPress` event etc. You can find details about the events emitted on the individual navigator's documentation.
The `unsubscribe` function can be returned as the cleanup function in the effect.
77
202
78
203
For class components, you can add the event in the `componentDidMount` lifecycle method and unsubscribe in `componentWillUnmount`:
@@ -113,6 +238,28 @@ Sometimes you might want to add a listener from the component where you defined
113
238
114
239
Example:
115
240
241
+
<TabsgroupId="config"queryString="config">
242
+
<TabItemvalue="static"label="Static"default>
243
+
244
+
```js
245
+
constTab=createBottomTabNavigatior({
246
+
screens: {
247
+
Chat: {
248
+
screen: Chat,
249
+
listeners: {
250
+
tabPress: (e) => {
251
+
// Prevent default action
252
+
e.preventDefault;
253
+
},
254
+
},
255
+
},
256
+
},
257
+
});
258
+
```
259
+
260
+
</TabItem>
261
+
<TabItemvalue="dynamic"label="Dynamic">
262
+
116
263
```js
117
264
<Tab.Screen
118
265
name="Chat"
@@ -126,9 +273,36 @@ Example:
126
273
/>
127
274
```
128
275
276
+
</TabItem>
277
+
</Tabs>
278
+
129
279
You can also pass a callback which returns the object with listeners. It'll receive `navigation` and `route` as the arguments.
130
280
131
281
Example:
282
+
<TabsgroupId="config"queryString="config">
283
+
<TabItemvalue="static"label="Static"default>
284
+
285
+
```js
286
+
constTab=createBottomTabNavigatior({
287
+
screens: {
288
+
Chat: {
289
+
screen: Chat,
290
+
listeners: ({ navigation, route }) => ({
291
+
tabPress: (e) => {
292
+
// Prevent default action
293
+
e.preventDefault;
294
+
295
+
// Do something with the `navigation` object
296
+
navigation.navigate('AnotherPlace');
297
+
},
298
+
}),
299
+
},
300
+
},
301
+
});
302
+
```
303
+
304
+
</TabItem>
305
+
<TabItemvalue="dynamic"label="Dynamic">
132
306
133
307
```js
134
308
<Tab.Screen
@@ -146,12 +320,36 @@ Example:
146
320
/>
147
321
```
148
322
323
+
</TabItem>
324
+
</Tabs>
325
+
149
326
### `screenListeners` prop on the navigator
150
327
151
328
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.
152
329
153
330
Example:
154
331
332
+
<TabsgroupId="config"queryString="config">
333
+
<TabItemvalue="static"label="Static"default>
334
+
335
+
```js
336
+
constStack=createNativeStackNavigator({
337
+
screenListeners: {
338
+
state: (e) => {
339
+
// Do something with the state
340
+
console.log('state changed', e.data);
341
+
},
342
+
},
343
+
screens: {
344
+
Home: HomeScreen,
345
+
Profile: ProfileScreen,
346
+
},
347
+
});
348
+
```
349
+
350
+
</TabItem>
351
+
<TabItemvalue="dynamic"label="Dynamic">
352
+
155
353
```js
156
354
<Stack.Navigator
157
355
screenListeners={{
@@ -166,8 +364,37 @@ Example:
166
364
</Stack.Navigator>
167
365
```
168
366
367
+
</TabItem>
368
+
</Tabs>
369
+
169
370
Similar to `listeners`, you can also pass a function to `screenListeners`. The function will receive the [`navigation` object](navigation-object.md) and the [`route` object](route-object.md) for each screen. This can be useful if you need access to the `navigation` object.
170
371
372
+
<TabsgroupId="config"queryString="config">
373
+
<TabItemvalue="static"label="Static"default>
374
+
375
+
```js
376
+
constTab=createBottomTabNavigatior({
377
+
screenListeners: ({ navigation }) => ({
378
+
state: (e) => {
379
+
// Do something with the state
380
+
console.log('state changed', e.data);
381
+
382
+
// Do something with the `navigation` object
383
+
if (!navigation.canGoBack()) {
384
+
console.log("we're on the initial screen");
385
+
}
386
+
},
387
+
}),
388
+
screens: {
389
+
Home: HomeScreen,
390
+
Profile: ProfileScreen,
391
+
},
392
+
});
393
+
```
394
+
395
+
</TabItem>
396
+
<TabItemvalue="dynamic"label="Dynamic">
397
+
171
398
```js
172
399
<Tab.Navigator
173
400
screenListeners={({ navigation }) => ({
@@ -186,3 +413,6 @@ Similar to `listeners`, you can also pass a function to `screenListeners`. The f
0 commit comments