{"record":{"id":"dc3f121dd288f4e3","repo":"DioxusLabs/dioxus","slug":"use-route-must-have-access-to-a-router","errorCode":null,"errorMessage":"use_route must have access to a router","messagePattern":"use_route must have access to a router","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/router/src/hooks/use_router.rs","lineNumber":7,"sourceCode":"use crate::{RouterContext, utils::use_router_internal::use_router_internal};\n\n#[deprecated = \"prefer the `router()` function or `use_route` functions\"]\n#[must_use]\n/// A hook that provides access to information about the router.\npub fn use_router() -> RouterContext {\n    use_router_internal().expect(\"use_route must have access to a router\")\n}\n\n/// Acquire the router without subscribing to updates.\n#[doc(alias = \"url\")]\npub fn router() -> RouterContext {\n    dioxus_core::consume_context()\n}\n\n/// Try to acquire the router without subscribing to updates.\n#[doc(alias = \"url\")]\npub fn try_router() -> Option<RouterContext> {\n    dioxus_core::try_consume_context()\n}\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/393d190a801ccb441d41923e232289b4f8a5c669/packages/router/src/hooks/use_router.rs#L1-L21","documentation":"Deprecated router-acquisition hook. use_router() (via use_router_internal) consumes the RouterContext during render and panics when no Router component is an ancestor. The non-deprecated replacement router() also panics without a Router, while try_router() returns an Option. The hook variant additionally subscribes the calling scope to router updates.","triggerScenarios":"Calling the deprecated use_router() in a component that is not a descendant of a Router; following pre-0.5 examples that call use_router in the top-level App component which itself renders the Router (context not yet provided at that point).","commonSituations":"Building against outdated docs or examples; code migrated from older dioxus versions where use_router was called at the app root.","solutions":["Replace use_router() with router()/try_router() called from a Router descendant, or the use_route APIs","Ensure the calling component is rendered by a route inside Router::<Route> {}","Restructure so the Router sits at the root and everything below it uses router APIs"],"exampleFix":"// before\nfn App() -> Element {\n    let router = use_router(); // App renders the Router itself -> panics\n    ...\n}\n// after\nfn Home() -> Element {\n    let router = dioxus::router::router(); // inside a route under Router\n    ...\n}","handlingStrategy":"type-guard","validationCode":"match dioxus::router::try_router() {\n    Some(router) => { /* use router */ }\n    None => { /* not inside a Router: skip or degrade gracefully */ }\n}","typeGuard":"fn try_use_router() -> Option<dioxus::router::RouterContext> {\n    dioxus::prelude::try_consume_context::<dioxus::router::RouterContext>()\n}","tryCatchPattern":null,"preventionTips":["Migrate off the deprecated use_router() to router()/try_router()/use_route APIs","Only call router APIs from components rendered by a route under Router::<Route> {}","When porting old examples, note that the component rendering Router must not itself query the router"],"tags":["router","hooks","deprecated","context","panic"],"backgroundTag":null,"analyzedSha":"393d190a801ccb441d41923e232289b4f8a5c669","analyzedAt":"2026-08-16T11:27:45.815Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}