Skip to main content

useBlocker

Allow the application to block navigations within the SPA and present the user a confirmation dialog to confirm the navigation. Mostly used to avoid losing half-filled form data.
This hook only works in Data and Framework modes.
This does not handle hard-reloads or cross-origin navigations. Use the browser’s beforeunload event for those cases.

Signature

Parameters

boolean | BlockerFunction
required
Either a boolean or a function that returns a boolean indicating whether the navigation should be blocked.When using the function format, it receives:
  • currentLocation - The current location
  • nextLocation - The location being navigated to
  • historyAction - The type of navigation (PUSH, REPLACE, or POP)

Returns

Blocker
An object with the following properties:

Usage

Block with boolean

Block with function

Proceed on form submit

Custom confirmation dialog

Common Patterns

Block only external navigation

Show destination in dialog

Auto-save before proceeding

Multi-step wizard

Block during async operations

Important Notes

Browser navigation

useBlocker only blocks in-app navigation (using <Link>, navigate(), etc.). It does NOT block:
  • Browser back/forward buttons (use beforeunload event)
  • Page refreshes (use beforeunload event)
  • Closing the tab/window (use beforeunload event)
  • External links
For those cases, use the browser’s beforeunload event:

Stable function reference

When using a function for shouldBlock, use useCallback to ensure a stable reference:

State transitions