Skip to main content

usePrompt

Wrapper around useBlocker to show a window.confirm prompt to users instead of building a custom UI with useBlocker.

Parameters

object
required
string
required
The message to show in the browser’s confirmation dialog when navigation is blocked.
boolean | BlockerFunction
required
A boolean or a function that returns a boolean indicating whether to block the navigation.If a function is provided, it receives an object with:
  • currentLocation - The current location
  • nextLocation - The location being navigated to
  • historyAction - The type of navigation (“PUSH”, “REPLACE”, or “POP”)

Type Declaration

Deprecation Notice

The unstable_ flag will not be removed because this technique has a lot of rough edges and behaves very differently (and incorrectly sometimes) across browsers if users click additional back/forward navigations while the confirmation is open. Use useBlocker instead for more reliable and customizable navigation blocking.

Migration to useBlocker

Before (using usePrompt)

After (using useBlocker)

Usage Examples (Legacy)

Basic Usage

Conditional Blocking

With Form State

Multiple Conditions

Known Issues

Browser Inconsistencies

  1. Multiple back/forward clicks: If users click back/forward multiple times while the prompt is open, behavior varies across browsers
  2. Mobile browsers: May not show the confirmation dialog reliably
  3. Browser dialogs: Cannot be styled or customized (uses native window.confirm)

Limitations

Why It’s Deprecated

  1. Unreliable: Browser behavior varies, especially with rapid navigation
  2. Poor UX: Native browser dialogs cannot be styled or customized
  3. Better alternative: useBlocker provides full control over the blocking experience
  4. Accessibility: Custom dialogs via useBlocker can be made more accessible
Use useBlocker for better control and reliability:

Notes

  • Deprecated: Use useBlocker instead
  • Available in Framework and Data modes only
  • Only blocks in-app navigation (not page reloads or tab closes)
  • Use useBeforeUnload to warn about page reloads/closes
  • Cannot customize the appearance of the browser’s confirmation dialog
  • Behavior is inconsistent across browsers