Skip to content

Commit e4ccad5

Browse files
committed
Update docs for event listeners
1 parent 9b55d63 commit e4ccad5

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed

versioned_docs/version-5.x/stack-navigator.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,54 @@ React.useEffect(() => {
463463
}, [navigation]);
464464
```
465465

466+
#### `gestureStart`
467+
468+
This event is fired when the swipe gesture starts for the current screen.
469+
470+
Example:
471+
472+
```js
473+
React.useEffect(() => {
474+
const unsubscribe = navigation.addListener('gestureStart', (e) => {
475+
// Do something
476+
});
477+
478+
return unsubscribe;
479+
}, [navigation]);
480+
```
481+
482+
#### `gestureEnd`
483+
484+
This event is fired when the swipe gesture ends for the current screen. e.g. a screen was successfully dismissed.
485+
486+
Example:
487+
488+
```js
489+
React.useEffect(() => {
490+
const unsubscribe = navigation.addListener('gestureEnd', (e) => {
491+
// Do something
492+
});
493+
494+
return unsubscribe;
495+
}, [navigation]);
496+
```
497+
498+
#### `gestureCancel`
499+
500+
This event is fired when the swipe gesture is cancelled for the current screen. e.g. a screen wasn't dismissed by the gesture.
501+
502+
Example:
503+
504+
```js
505+
React.useEffect(() => {
506+
const unsubscribe = navigation.addListener('gestureCancel', (e) => {
507+
// Do something
508+
});
509+
510+
return unsubscribe;
511+
}, [navigation]);
512+
```
513+
466514
### Helpers
467515

468516
The stack navigator adds the following methods to the navigation prop:

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Each callback registered as an event listener receive an event object as its arg
2323

2424
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.
2525

26-
There are 2 ways to listen to events:
26+
There are 3 ways to listen to events:
2727

2828
### `navigation.addListener`
2929

@@ -32,7 +32,7 @@ Inside a screen, you can add listeners on the `navigation` prop with the `addLis
3232
Example:
3333

3434
```js
35-
const unsubscribe = navigation.addListener('tabPress', e => {
35+
const unsubscribe = navigation.addListener('tabPress', (e) => {
3636
// Prevent default action
3737
e.preventDefault();
3838
});
@@ -89,7 +89,7 @@ Example:
8989
name="Chat"
9090
component={Chat}
9191
listeners={{
92-
tabPress: e => {
92+
tabPress: (e) => {
9393
// Prevent default action
9494
e.preventDefault();
9595
},
@@ -106,7 +106,7 @@ Example:
106106
name="Chat"
107107
component={Chat}
108108
listeners={({ navigation, route }) => ({
109-
tabPress: e => {
109+
tabPress: (e) => {
110110
// Prevent default action
111111
e.preventDefault();
112112

@@ -116,3 +116,44 @@ Example:
116116
})}
117117
/>
118118
```
119+
120+
### `screenListeners` prop on the navigator
121+
122+
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.
123+
124+
Example:
125+
126+
```js
127+
<Stack.Navigator
128+
screenListeners={{
129+
state: (e) => {
130+
// Do something with the state
131+
console.log('state changed', e.data);
132+
},
133+
}}
134+
>
135+
<Stack.Screen name="Home" component={HomeScreen} />
136+
<Stack.Screen name="Profile" component={ProfileScreen} />
137+
</Stack.Navigator>
138+
```
139+
140+
Similar to `listeners`, you can also pass a function to `screenListeners`. The function will receive the [`navigation` prop](navigation-prop.md) and the [`route` prop](route-prop.md) for each screen. This can be useful if you need access to the `navigation` object.
141+
142+
```js
143+
<Tab.Navigator
144+
screenListeners={({ navigation }) => ({
145+
state: (e) => {
146+
// Do something with the state
147+
console.log('state changed', e.data);
148+
149+
// Do something with the `navigation` object
150+
if (!navigation.canGoBack()) {
151+
console.log("we're on the initial screen");
152+
}
153+
},
154+
})}
155+
>
156+
<Tab.Screen name="Home" component={HomeScreen} />
157+
<Tab.Screen name="Profile" component={ProfileScreen} />
158+
</Tab.Navigator>
159+
```

versioned_docs/version-6.x/stack-navigator.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,54 @@ React.useEffect(() => {
324324
}, [navigation]);
325325
```
326326

327+
#### `gestureStart`
328+
329+
This event is fired when the swipe gesture starts for the current screen.
330+
331+
Example:
332+
333+
```js
334+
React.useEffect(() => {
335+
const unsubscribe = navigation.addListener('gestureStart', (e) => {
336+
// Do something
337+
});
338+
339+
return unsubscribe;
340+
}, [navigation]);
341+
```
342+
343+
#### `gestureEnd`
344+
345+
This event is fired when the swipe gesture ends for the current screen. e.g. a screen was successfully dismissed.
346+
347+
Example:
348+
349+
```js
350+
React.useEffect(() => {
351+
const unsubscribe = navigation.addListener('gestureEnd', (e) => {
352+
// Do something
353+
});
354+
355+
return unsubscribe;
356+
}, [navigation]);
357+
```
358+
359+
#### `gestureCancel`
360+
361+
This event is fired when the swipe gesture is cancelled for the current screen. e.g. a screen wasn't dismissed by the gesture.
362+
363+
Example:
364+
365+
```js
366+
React.useEffect(() => {
367+
const unsubscribe = navigation.addListener('gestureCancel', (e) => {
368+
// Do something
369+
});
370+
371+
return unsubscribe;
372+
}, [navigation]);
373+
```
374+
327375
### Helpers
328376

329377
The stack navigator adds the following methods to the navigation prop:

0 commit comments

Comments
 (0)