{"record":{"id":"5ab4d31716240a4b","repo":"leptos-rs/leptos","slug":"tried-to-access-location-outside-a-router","errorCode":null,"errorMessage":"Tried to access Location outside a <Router>.","messagePattern":"Tried to access Location outside a <Router>\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"router/src/hooks.rs","lineNumber":173,"sourceCode":"#[track_caller]\npub(crate) fn use_router() -> RouterContext {\n    if let Some(router) = use_context::<RouterContext>() {\n        router\n    } else {\n        leptos::leptos_dom::debug_warn!(\n            \"You must call use_router() within a <Router/> component {:?}\",\n            std::panic::Location::caller()\n        );\n        panic!(\"You must call use_router() within a <Router/> component\");\n    }\n}\n*/\n\n/// Returns the current [`Location`], which contains reactive variables\n#[track_caller]\npub fn use_location() -> Location {\n    let RouterContext { location, .. } =\n        use_context().expect(\"Tried to access Location outside a <Router>.\");\n    location\n}\n\npub(crate) type RawParamsMap = ArcMemo<ParamsMap>;\n\n#[track_caller]\nfn use_params_raw() -> RawParamsMap {\n    use_context().expect(\n        \"Tried to access params outside the context of a matched <Route>.\",\n    )\n}\n\n/// Returns a raw key-value map of route params.\n#[track_caller]\npub fn use_params_map() -> Memo<ParamsMap> {\n    use_params_raw().into()\n}\n","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/router/src/hooks.rs#L155-L191","documentation":"`use_location()` reads the `Location` reactive struct from router context. It panics with this message when there is no `<Router>` ancestor providing a `RouterContext`, meaning the hook was called outside the router's reactive tree.","triggerScenarios":"Calling `use_location()` in a component rendered above/outside `<Router>` (e.g. in App root before the router mounts), inside a portal/teleport mounted outside the router owner, or in tests/components without a router wrapper.","commonSituations":"Building a Navbar component that lives outside `<Router>`; rendering components in a test harness without `<Router>`; wrapping the router inside another component but calling hooks from the wrapper.","solutions":["Move the component calling `use_location()` inside the `<Router>` tree.","If the component must stay outside, wrap the app so `<Router>` is the outermost component and pass location-derived values down as props/signals.","In tests, render the component within a `<Router>` (or a MemoryRouter) provider."],"exampleFix":"// before\nview! {\n  <NavBar/>        // uses use_location() -> panics\n  <Router>\n    <Routes>...</Routes>\n  </Router>\n}\n\n// after\nview! {\n  <Router>\n    <NavBar/>\n    <Routes>...</Routes>\n  </Router>\n}","handlingStrategy":"fallback","validationCode":"// Check context availability before using the hook inside a component:\nlet has_router = use_context::<RouterContext>().is_some();","typeGuard":"fn has_router_context() -> bool { use_context::<RouterContext>().is_some() }","tryCatchPattern":null,"preventionTips":["Keep <Router> as the outermost component of the app tree.","Only call router hooks from components rendered inside <Router>.","In tests, always wrap components under test in a <Router>."],"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"}