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
+44-1
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,50 @@ This event is emitted when the navigator's state changes. This event receives th
31
31
32
32
### `beforeRemove`
33
33
34
-
This event is emitted when the user is leaving the screen, there's a chance to [prevent the user from leaving](preventing-going-back.md).
34
+
This event is emitted when the user is leaving the screen due to a navigation action. It is possible to prevent the user from leaving the screen by calling `e.preventDefault()` in the event listener.
35
+
36
+
```js
37
+
React.useEffect(
38
+
() =>
39
+
navigation.addListener('beforeRemove', (e) => {
40
+
if (!hasUnsavedChanges) {
41
+
return;
42
+
}
43
+
44
+
// Prevent default behavior of leaving the screen
45
+
e.preventDefault();
46
+
47
+
// Prompt the user before leaving the screen
48
+
Alert.alert(
49
+
'Discard changes?',
50
+
'You have unsaved changes. Are you sure to discard them and leave the screen?',
51
+
[
52
+
{
53
+
text:"Don't leave",
54
+
style:'cancel',
55
+
onPress: () => {
56
+
// Do nothing
57
+
},
58
+
},
59
+
{
60
+
text:'Discard',
61
+
style:'destructive',
62
+
// If the user confirmed, then we dispatch the action we blocked earlier
63
+
// This will continue the action that had triggered the removal of the screen
64
+
onPress: () =>navigation.dispatch(e.data.action),
65
+
},
66
+
]
67
+
);
68
+
}),
69
+
[navigation, hasUnsavedChanges]
70
+
);
71
+
```
72
+
73
+
:::note
74
+
75
+
Preventing the action in this event doesn't work properly with [`@react-navigation/native-stack`](native-stack-navigator.md). We recommend using the [`usePreventRemove` hook](preventing-going-back.md) instead.
Copy file name to clipboardExpand all lines: versioned_docs/version-7.x/preventing-going-back.md
+66-246
Original file line number
Diff line number
Diff line change
@@ -7,270 +7,90 @@ sidebar_label: Preventing going back
7
7
import Tabs from '@theme/Tabs';
8
8
import TabItem from '@theme/TabItem';
9
9
10
-
Sometimes you may want to prevent the user from leaving a screen, for example, if there are unsaved changes, you might want to show a confirmation dialog. You can achieve it by using the `beforeRemove` event.
10
+
Sometimes you may want to prevent the user from leaving a screen to avoid losing unsaved changes. There are a couple of things you may want to do in this case:
11
11
12
-
The event listener receives the `action` that triggered it. You can dispatch this action again after confirmation, or check the action object to determine what to do.
12
+
## Prevent the user from leaving the screen
13
13
14
-
Example:
14
+
The `usePreventRemove` hook allows you to prevent the user from leaving a screen. See the [`usePreventRemove`](use-prevent-remove.md) docs for more details.
There are couple of limitations to be aware of when using the `beforeRemove` event. The event is **only** triggered whenever a screen is being removed due to a navigation state change. For example:
70
+
return () =>backHandler.remove();
71
+
}, []);
72
+
```
257
73
258
-
- The user pressed back button on a screen in a stack.
259
-
- The user performed a swipe back gesture.
260
-
- Some action such as `pop` or `reset` was dispatched which removes the screen from the state.
74
+
On the Web, you can use the `beforeunload` event to prompt the user before they leave the browser tab:
261
75
262
-
This event is **not** triggered when a screen is being unfocused but not removed. For example:
76
+
```js
77
+
React.useEffect(() => {
78
+
constonBeforeUnload= (event) => {
79
+
// Prevent the user from leaving the page
80
+
event.preventDefault();
81
+
event.returnValue=true;
82
+
};
263
83
264
-
- The user pushed a new screen on top of the screen with the listener in a stack.
265
-
- The user navigated from one tab/drawer screen to another tab/drawer screen.
- The user closes the app (e.g. by pressing the back button on the home screen, closing the tab in the browser, closing it from app switcher etc.). You can additionally use [`hardwareBackPress`](https://reactnative.dev/docs/backhandler) event on Android, [`beforeunload`](https://developer.mozilla.org/en-US/docs/web/api/window/beforeunload_event) event on Web etc. to handle some of these cases.
270
-
- A screen gets unmounted due to conditional rendering, or due to a parent component being unmounted.
271
-
- A screen gets unmounted due to usage of `unmountOnBlur` options with [`@react-navigation/bottom-tabs`](bottom-tab-navigator.md), [`@react-navigation/drawer`](drawer-navigator.md) etc.
92
+
:::warning
272
93
273
-
In addition to the above scenarios, this feature also doesn't work properly with [`@react-navigation/native-stack`](native-stack-navigator.md). To make this work, you need to:
94
+
The user can still close the app by swiping it away from the app switcher or closing the browser tab. Or the app can be closed by the system due to low memory or other reasons. It's also not possible to prevent leaving the app on iOS. We recommend persisting the data and restoring it when the app is opened again instead of prompting the user before they leave the app.
274
95
275
-
- Disable the swipe gesture for the screen (`gestureEnabled: false`).
276
-
- Override the native back button in the header with a custom back button (`headerLeft: (props) => <CustomBackButton {...props} />`).
0 commit comments