DioxusLabs/dioxus · error

Could not find context {}

Error message

Could not find context {}

What it means

consume_context looked up a context of the requested type in the current scope's runtime and found none. A provider for that type must exist in an ancestor scope (via provide_context) before it can be consumed; a missing or differently-typed provider causes this failure.

Source

Thrown at packages/core/src/global_context.rs:51

/// Try to consume context from the current scope, returning `None` if not found.
///
/// Unlike `use_context`, this is **not** a hook. It reads the context value directly from the
/// runtime each time it is called, rather than caching it on first render. This means it can be
/// called from anywhere the Dioxus runtime is active — inside event handlers, async tasks, spawned
/// futures, or other non-hook contexts — without following the rules of hooks.
pub fn try_consume_context<T: 'static + Clone>() -> Option<T> {
    Runtime::with_current_scope(|cx| cx.consume_context::<T>())
}

/// Consume context from the current scope, panicking if not found.
///
/// Unlike `use_context`, this is **not** a hook. It reads the context value directly from the
/// runtime each time it is called, rather than caching it on first render. This means it can be
/// called from anywhere the Dioxus runtime is active — inside event handlers, async tasks, spawned
/// futures, or other non-hook contexts — without following the rules of hooks.
pub fn consume_context<T: 'static + Clone>() -> T {
    Runtime::with_current_scope(|cx| cx.consume_context::<T>())
        .unwrap_or_else(|| panic!("Could not find context {}", std::any::type_name::<T>()))
}

/// Consume context from the current scope
pub fn consume_context_from_scope<T: 'static + Clone>(scope_id: ScopeId) -> Option<T> {
    Runtime::current()
        .try_get_state(scope_id)
        .and_then(|cx| cx.consume_context::<T>())
}

/// Check if the current scope has a context
pub fn has_context<T: 'static + Clone>() -> Option<T> {
    Runtime::with_current_scope(|cx| cx.has_context::<T>())
}

/// Provide context to the current scope
pub fn provide_context<T: 'static + Clone>(value: T) -> T {
    Runtime::with_current_scope(|cx| cx.provide_context(value))
}

View on GitHub (pinned to 24f6a829df)

Solutions

  1. Provide the context in an ancestor component with use_context_provider before consuming it with use_context or try_use_context.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at packages/core/src/global_context.rs:51 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of DioxusLabs/dioxus@24f6a829df (2026-08-23). Data as JSON: /api/errors/4ab4656b77d7e04d. Report an issue: GitHub.