Guards decide who may open a screen. The opposite problem is a screen that shouldn't close yet, such as an editor holding text the user hasn't saved. usePreventRemove from expo-router blocks the removal and hands your callback the blocked action, so you can ask the user first and then finish that action or go somewhere else.
Key terms
- usePreventRemove: a hook exported by
expo-routerthat keeps the screen from being removed while its boolean istrueand calls a callback with the blocked navigation action. - repeat: the function passed to that callback, which disables prevention and dispatches the blocked action again.
- disablePrevention: the function
usePreventRemovereturns, used to cancel the blocked action and navigate somewhere else. - removePrevented: the navigation event for an action that
usePreventRemoveblocked.
A boolean that holds the screen
Its signature:
tsxusePreventRemove(preventRemove: boolean, callback?, options?): () => void
While preventRemove is true, navigation can't remove the screen, and the callback receives { data: { action }, repeat }, where action is the navigation action that was blocked. The callback only runs when preventRemove is true and you passed a callback. Call the hook inside a route's screen or layout. Anywhere else it throws Couldn't find the prevent remove context. Is your component inside Screen or Layout?
The optional third argument is a PreventRemoveOptions object, a type expo-router also exports.
The hook is new to expo-router in SDK 58, and the beforeRemove and __unsafe_action__ events are gone. To observe a completed removal, listen for removed. To observe a blocked action, listen for removePrevented.
On web, the hook also asks the browser to confirm before a refresh, closing the tab or navigating to another site. The browser decides whether a dialog appears and what it says.
Discarding a note
In the EditNoteScreen example, typing sets isDirty to true and Save sets it back to false. While it is true, navigation that would remove the screen, such as going back, is blocked, and the callback asks what to do. On web it uses window.confirm. On Android and iOS it shows an Alert with a Keep writing button, which does nothing and leaves the screen open, and a Discard button that calls this handler:
tsxconst discard = () => { setIsDirty(false); repeat(); };
Set the boolean to false before you call repeat(). That order keeps your state in sync with removal prevention. repeat() disables prevention and dispatches the blocked action again, so the same back navigation that was stopped now completes and the screen closes.
The boolean doesn't need its own state. You can derive it from the form, as in const hasText = text.length > 0, and pass hasText to the hook. The Discard handler then clears the text before it calls repeat(), so the derived value is false as well.
Leaving with disablePrevention
repeat() continues the exact action that was blocked. To send the user somewhere else, keep the function the hook returns, set the boolean to false, call disablePrevention(), and then navigate. That is what the Back to all notes button does in startOver:
tsxsetIsDirty(false); disablePrevention(); router.replace('/notes');
disablePrevention also works without a callback. A screen that only needs a Leave button can call usePreventRemove(isDirty) and use the returned function.
Skipping the state update causes a quiet bug. If the boolean stays true after repeat() or disablePrevention(), the screen is no longer protected, and the hook won't turn prevention back on until the boolean changes. Development builds log a warning about it, and production builds skip that check entirely.
Leave the dispatching to repeat() too. A navigation action dispatched from a raw removePrevented listener while prevention is still active can be blocked again.
Code Examples
import { usePreventRemove, useRouter } from 'expo-router';
import { useState } from 'react';
import { Alert, Button, Platform, TextInput, View } from 'react-native';
export default function EditNoteScreen() {
const router = useRouter();
const [text, setText] = useState('');
const [isDirty, setIsDirty] = useState(false);
const disablePrevention = usePreventRemove(isDirty, ({ repeat }) => {
const discard = () => {
setIsDirty(false);
repeat();
};
if (Platform.OS === 'web') {
if (window.confirm('Discard this note?')) {
discard();
}
return;
}
Alert.alert('Discard this note?', 'Your edits have not been saved.', [
{ text: 'Keep writing', style: 'cancel' },
{ text: 'Discard', onPress: discard },
]);
});
function startOver() {
setIsDirty(false);
disablePrevention();
router.replace('/notes');
}
return (
<View style={{ flex: 1, padding: 16, gap: 12 }}>
<TextInput
value={text}
onChangeText={(value) => {
setText(value);
setIsDirty(true);
}}
placeholder="Write a note"
multiline
/>
<Button title="Save" onPress={() => setIsDirty(false)} />
<Button title="Back to all notes" onPress={startOver} />
</View>
);
}