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.
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 locationnextLocation- The location being navigated tohistoryAction- 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
beforeunloadevent) - Page refreshes (use
beforeunloadevent) - Closing the tab/window (use
beforeunloadevent) - External links
beforeunload event:
Stable function reference
When using a function forshouldBlock, use useCallback to ensure a stable reference:
State transitions
Related
useNavigate- Navigate programmaticallyuseNavigation- Track navigation stateForm- Form component