NavigationBlocker
Storybook
Go to StoryNavigationBlockerProps
Defined in: src/components/NavigationBlocker/NavigationBlocker.tsx:23
Extends
Omit<ComponentProps<typeofDialog>,"open">
Properties
| Property | Type | Description | Inherited from | Defined in |
|---|---|---|---|---|
children? | ReactNode | - | Omit.children | node_modules/@radix-ui/react-dialog/dist/index.d.mts:10 |
defaultOpen? | boolean | - | Omit.defaultOpen | node_modules/@radix-ui/react-dialog/dist/index.d.mts:12 |
onOpenChange? | (open) => void | - | Omit.onOpenChange | node_modules/@radix-ui/react-dialog/dist/index.d.mts:13 |
modal? | boolean | - | Omit.modal | node_modules/@radix-ui/react-dialog/dist/index.d.mts:14 |
enableBeforeUnload? | boolean | Controls whether the native browser beforeunload prompt should be enabled. When true and shouldBlock is true, a native prompt will appear on page reload/close. Default false | - | src/components/NavigationBlocker/NavigationBlocker.tsx:31 |
shouldBlock? | boolean | Indicates whether there are unsaved changes that should block navigation. | - | src/components/NavigationBlocker/NavigationBlocker.tsx:33 |
status? | BlockerStatus | When set to "blocked", the confirmation dialog is shown. | - | src/components/NavigationBlocker/NavigationBlocker.tsx:35 |
proceed? | () => void | When status is blocked, this function allows navigation to continue. | - | src/components/NavigationBlocker/NavigationBlocker.tsx:37 |
reset? | () => void | When status is blocked, this function cancels navigation (status will be reset to 'idle') | - | src/components/NavigationBlocker/NavigationBlocker.tsx:39 |
title? | ReactNode | - | - | src/components/NavigationBlocker/NavigationBlocker.tsx:40 |
description? | ReactNode | - | - | src/components/NavigationBlocker/NavigationBlocker.tsx:41 |
stayLabel? | ReactNode | - | - | src/components/NavigationBlocker/NavigationBlocker.tsx:42 |
leaveLabel? | ReactNode | - | - | src/components/NavigationBlocker/NavigationBlocker.tsx:43 |
BlockerStatus
type BlockerStatus = "idle" | "blocked";
Defined in: src/components/NavigationBlocker/NavigationBlocker.tsx:21
NavigationBlocker()
function NavigationBlocker(__namedParameters): Element;
Defined in: src/components/NavigationBlocker/NavigationBlocker.tsx:92
A small confirmation dialog to guard against accidental navigation when there are unsaved changes. This component is router-agnostic. It does not import or depend on any specific router library.
Parameters
| Parameter | Type |
|---|---|
__namedParameters | NavigationBlockerProps |
Returns
Element
Examples
import { useBlocker } from "@tanstack/react-router";
const blocker = useBlocker({
shouldBlockFn: (location, action) => {
// block if there are unsaved changes and the user is trying to navigate away
return formState.isDirty && action !== "replace";
},
withResolver: true,
enableBeforeUnload: true,
});
return (
<NavigationBlocker
status={blocker.status}
proceed={blocker.proceed}
reset={blocker.reset}
/>
);
// The `enableBeforeUnload` prop enables a native browser prompt on page reload/close.
const [dirty, setDirty] = useState(false);
return (
<>
<NavigationBlocker shouldBlock={dirty} enableBeforeUnload />
<form
onChange={() => setDirty(true)}
onSubmit={(e) => {
e.preventDefault();
// save...
setDirty(false);
}}
>
<input name="displayName" />
<button type="submit">Save</button>
</form>
</>
);