DioxusLabs/dioxus · error

use_route must have access to a router

Error message

use_route must have access to a router

What it means

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.

Source

Thrown at packages/router/src/hooks/use_router.rs:7

use crate::{RouterContext, utils::use_router_internal::use_router_internal};

#[deprecated = "prefer the `router()` function or `use_route` functions"]
#[must_use]
/// A hook that provides access to information about the router.
pub fn use_router() -> RouterContext {
    use_router_internal().expect("use_route must have access to a router")
}

/// Acquire the router without subscribing to updates.
#[doc(alias = "url")]
pub fn router() -> RouterContext {
    dioxus_core::consume_context()
}

/// Try to acquire the router without subscribing to updates.
#[doc(alias = "url")]
pub fn try_router() -> Option<RouterContext> {
    dioxus_core::try_consume_context()
}

View on GitHub (pinned to 393d190a80)

Solutions

  1. Replace use_router() with router()/try_router() called from a Router descendant, or the use_route APIs
  2. Ensure the calling component is rendered by a route inside Router::<Route> {}
  3. Restructure so the Router sits at the root and everything below it uses router APIs

Example fix

// before
fn App() -> Element {
    let router = use_router(); // App renders the Router itself -> panics
    ...
}
// after
fn Home() -> Element {
    let router = dioxus::router::router(); // inside a route under Router
    ...
}
Defensive patterns

Strategy: type-guard

Validate before calling

match dioxus::router::try_router() {
    Some(router) => { /* use router */ }
    None => { /* not inside a Router: skip or degrade gracefully */ }
}

Type guard

fn try_use_router() -> Option<dioxus::router::RouterContext> {
    dioxus::prelude::try_consume_context::<dioxus::router::RouterContext>()
}

Prevention

When it happens

Trigger: 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).

Common situations: Building against outdated docs or examples; code migrated from older dioxus versions where use_router was called at the app root.

Related errors


AI-assisted analysis of DioxusLabs/dioxus@393d190a80 (2026-08-16). Data as JSON: /api/errors/dc3f121dd288f4e3. Report an issue: GitHub.