{"record":{"id":"54d899599aec00dd","repo":"leptos-rs/leptos","slug":"use-matched-called-outside-a-matched-route","errorCode":null,"errorMessage":"use_matched called outside a matched Route","messagePattern":"use_matched called outside a matched Route","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"router/src/hooks.rs","lineNumber":286,"sourceCode":"/// ```rust\n/// # if false { // can't actually navigate, no <Router/>\n/// let navigate = leptos_router::hooks::use_navigate();\n/// navigate(\"/\", Default::default());\n/// # }\n/// ```\n#[track_caller]\npub fn use_navigate() -> impl Fn(&str, NavigateOptions) + Clone {\n    let cx = use_context::<RouterContext>()\n        .expect(\"You cannot call `use_navigate` outside a <Router>.\");\n    move |path: &str, options: NavigateOptions| cx.navigate(path, options)\n}\n\n/// Returns a reactive string that contains the route that was matched for\n/// this [`Route`](crate::components::Route).\n#[track_caller]\npub fn use_matched() -> Memo<String> {\n    use_context::<Matched>()\n        .expect(\"use_matched called outside a matched Route\")\n        .0\n        .into()\n}\n","sourceCodeStart":268,"sourceCodeEnd":290,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/router/src/hooks.rs#L268-L290","documentation":"use_matched returns the params of the currently matched Route and only works inside a component rendered within a matched route's Outlet context. Calling it where no route has matched (above the Routes tree, outside Router, or during 404 handling) panics.","triggerScenarios":"Calling `use_matched()` in a component rendered outside any route's render tree (e.g. a component above `<Routes>`, or a route that did not match). Also hit by components like `Redirect` used outside route context.","commonSituations":"Using `use_matched` in a global layout that isn't itself inside a `<Route>`/parent route view; using it in 404/fallback components that render without a matched route; tests without routing setup.","solutions":["Call `use_matched()` only from components rendered inside a matched route's view/outlet.","If you need the current path outside a route, use `use_location()` inside a `<Router>` instead.","For fallback/404 views, provide them as a route definition so they still have Matched context."],"exampleFix":"// before\nview! {\n  <Router>\n    <Breadcrumbs/> // uses use_matched() -> panics (no route context)\n    <Routes/>\n  </Router>\n}\n\n// after\nview! {\n  <Router>\n    <Routes>\n      <Route path=path!(\"/:id\") view=move || view! { <Breadcrumbs/><Profile/> }/>\n    </Routes>\n  </Router>\n}","handlingStrategy":"fallback","validationCode":"let matched = use_context::<Matched>();\nlet path = matched.map(|m| m.0.get()).unwrap_or_default();","typeGuard":"fn matched_available() -> bool { use_context::<Matched>().is_some() }","tryCatchPattern":null,"preventionTips":["Call use_matched() only from route view components.","Prefer use_location() for path info outside routes.","Provide 404s as route definitions so they carry Matched context."],"tags":["react-hooks","router-context","leptos"],"backgroundTag":"hook-called-outside-router","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}