{"record":{"id":"2d868a4227b67751","repo":"leptos-rs/leptos","slug":"called-use-resolved-path-outside-a-router","errorCode":null,"errorMessage":"called use_resolved_path outside a <Router>","messagePattern":"called use_resolved_path outside a <Router>","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"router/src/hooks.rs","lineNumber":245,"sourceCode":"#[track_caller]\npub fn use_query<T>() -> Memo<Result<T, ParamsError>>\nwhere\n    T: Params + PartialEq + Send + Sync + 'static,\n{\n    let url = use_url_raw();\n    Memo::new(move |_| url.with(|url| T::from_map(url.search_params())))\n}\n\n#[derive(Debug, Clone)]\npub(crate) struct Matched(pub ArcMemo<String>);\n\n/// Resolves the given path relative to the current route.\n#[track_caller]\npub(crate) fn use_resolved_path(\n    path: impl Fn() -> String + Send + Sync + 'static,\n) -> ArcMemo<String> {\n    let router = use_context::<RouterContext>()\n        .expect(\"called use_resolved_path outside a <Router>\");\n    // TODO make this work with flat routes too?\n    let matched = use_context::<Matched>().map(|n| n.0);\n    ArcMemo::new(move |_| {\n        let path = path();\n        if path.starts_with('/') {\n            path\n        } else {\n            router\n                .resolve_path(\n                    &path,\n                    matched.as_ref().map(|n| n.get()).as_deref(),\n                )\n                .to_string()\n        }\n    })\n}\n\n/// Returns a function that can be used to navigate to a new route.","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/router/src/hooks.rs#L227-L263","documentation":"`use_resolved_path` is a crate-internal hook used by `<A>` and `<Form>` that pulls `RouterContext` to resolve a path relative to the current route. It panics when no `<Router>` ancestor provides that context.","triggerScenarios":"Rendering `<A href=...>` or `<Form ...>` outside a `<Router>`; calling the internal hook from custom components used above the router; portals rendering links outside the router owner.","commonSituations":"A shared Link component rendered in a modal or root layout outside the router; test harnesses without `<Router>`; migrating an app and moving the router so links end up above it in the tree.","solutions":["Render the `<A>`/`<Form>` component inside the `<Router>` tree.","Restructure so navigation components are children of the router, or use plain `<a>` tags for static links that don't need routing.","In tests, wrap the component under test in a `<Router>`."],"exampleFix":"// before\nview! {\n  <A href=\"/about\">About</A>\n  <Router><Routes/></Router>\n}\n\n// after\nview! {\n  <Router>\n    <A href=\"/about\">About</A>\n    <Routes/>\n  </Router>\n}","handlingStrategy":"fallback","validationCode":"let has_router = use_context::<RouterContext>().is_some();\nif !has_router { return view! { <a href={path}>{children}</a> }.into_any(); }","typeGuard":"fn router_available() -> bool { use_context::<RouterContext>().is_some() }","tryCatchPattern":null,"preventionTips":["Render <A>/<Form> only inside the router tree.","Avoid portaling components that use router hooks outside the router owner.","Use plain <a> for static links outside routed areas."],"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"}