Skip to main content

usePreventRemove

Prevent a server navigation action from removing the current page.

Import​

import { usePreventRemove } from '@bedrock-core/navigation';

Signature​

usePreventRemove(enabled: boolean, callback: (event) => void): void

Parameters​

ParameterTypeDefaultDescription
enabled*boolean—Block route removal while true
callback*({ data: { action } }) => void—Receive the blocked action

Returns​

void

Usage​

function Editor(): JSX.Element {
usePreventRemove(true, event => {
console.warn(`Blocked ${event.data.action.type}`);
});
return <Text>{'Save before leaving'}</Text>;
}

Notes​

The screen, a containing screen, or the root must use server navigation. Disable the guard before dispatching a previously blocked action.

This guard handles route-removing actions. Dismissing the native form closes its host and does not dispatch route Back.