{
  "id": "6fe7afa2b3c889c8c845482a",
  "source": "solid:signals",
  "type": "github-document",
  "title": "use-before-leave",
  "content": "---\ntitle: useBeforeLeave\nuse_cases: \u003e-\n  unsaved forms, exit confirmations, data loss prevention, user prompts before\n  navigation, dirty state handling\ntags:\n  - navigation\n  - forms\n  - confirmation\n  - prevent\n  - prompt\n  - leave\nversion: '1.0'\ndescription: \u003e-\n  Prevent route navigation with useBeforeLeave - prompt users before leaving\n  with unsaved changes, handle exit confirmations in SolidJS.\n---\n\n`useBeforeLeave` takes a function that will be called prior to leaving a route.\nThe function will be called with:\n\n- from (_Location_): current location (before change).\n- to (_string | number_}: path passed to `navigate.`\n- options (_NavigateOptions_}: options passed to `navigate.`\n- preventDefault (_void function_): call to block the route change.\n- defaultPrevented (_readonly boolean_): `true` if any previously called leave handlers called `preventDefault()`.\n- retry (_void function_, _force?: boolean_ ): call to retry the same navigation.\n  Pass `true` to skip running the leave handlers again (ie. force navigate without confirming).\n\nExample usage:\n\n```js\nuseBeforeLeave((e: BeforeLeaveEventArgs) =\u003e {\n\tif (form.isDirty \u0026\u0026 !e.defaultPrevented) {\n\t\t// preventDefault to block immediately and prompt user async\n\t\te.preventDefault();\n\t\tsetTimeout(() =\u003e {\n\t\t\tif (window.confirm(\"Discard unsaved changes - are you sure?\")) {\n\t\t\t\t// user wants to proceed anyway so retry with force=true\n\t\t\t\te.retry(true);\n\t\t\t}\n\t\t}, 100);\n\t}\n});\n```",
  "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-router/reference/primitives/use-before-leave.mdx",
  "metadata": {
    "path": "src/routes/solid-router/reference/primitives/use-before-leave.mdx",
    "repo": "solidjs/solid-docs",
    "repo_url": "https://github.com/solidjs/solid-docs.git",
    "size": 1449,
    "source_type": "github"
  },
  "hash": "08f586f60a08c0a6790a8245a8635c1efe87d0290e0208ecee32b643cb1f42d2",
  "timestamp": "2026-02-23T11:43:00.192971327+01:00"
}