{"record":{"id":"605f0374e6972c7c","repo":"leptos-rs/leptos","slug":"location-expected-context-of-type-type-name","errorCode":null,"errorMessage":"{location:?} expected context of type {type_name:?} to be present","messagePattern":"(.+?) expected context of type (.+?) to be present","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"reactive_graph/src/owner/context.rs","lineNumber":306,"sourceCode":"///         let value = use_context::<String>()\n///             .expect(\"could not find String in context\");\n///         assert_eq!(value, \"foo\");\n///         let value2 = use_context::<String>()\n///             .expect(\"could not find String in context\");\n///         assert_eq!(value2, \"foo\");\n///     });\n/// });\n/// # });\n/// ```\n/// ## Panics\n/// Panics if a context of this type is not found in the current reactive\n/// owner or its ancestors.\n#[track_caller]\npub fn expect_context<T: Clone + 'static>() -> T {\n    let location = std::panic::Location::caller();\n\n    use_context().unwrap_or_else(|| {\n        panic!(\n            \"{:?} expected context of type {:?} to be present\",\n            location,\n            std::any::type_name::<T>()\n        )\n    })\n}\n\n/// Extracts a context value of type `T` from the reactive system, and takes ownership,\n/// removing it from the context system.\n///\n/// This traverses the reactive ownership graph, beginning from the current reactive\n/// [`Owner`] and iterating through its parents, if any. When the value is found, it is removed,\n/// and is not available to any other [`use_context`] or [`take_context`] calls.\n///\n/// If the value is `Clone`, use [`use_context`] instead.\n///\n/// The context value should have been provided elsewhere using\n/// [`provide_context`](provide_context).","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/reactive_graph/src/owner/context.rs#L288-L324","documentation":"expect_context<T>() panics when no context value of type T has been provided by any owner in the current reactive ownership tree. Leptos context is provided with provide_context and consumed via use_context/expect_context; use_context returns Option, while expect_context is the assertive variant that assumes the context must exist. This panic means the consumer is running outside the subtree (or owner) where that context was provided.","triggerScenarios":"Calling expect_context::<T>() (directly or indirectly via query_signal_with_options) in a component, signal getter, or closure whose owner was not created inside a component that called provide_context::<T>() — e.g. calling it before the provider mounts, in a root that is not nested under the provider, or in an async task spawned with the wrong Owner.","commonSituations":"Using QuerySignal (leptos_query) at the app root without wrapping the app in the query client provider; rendering a component both inside and outside the provider (e.g. in a portal or router fallback); spawning a task with tokio::spawn instead of spawn_local, losing the owner; hydration mismatch where the provider is conditionally rendered.","solutions":["Wrap the consuming component (or the whole app) in the provider that calls provide_context for the required type (e.g. QueryClientProvider for QuerySignal).","Replace expect_context with use_context::<T>() and handle the None case gracefully instead of panicking.","If used in a spawned task, ensure it inherits the correct Owner via Owner::with_current or provide the context explicitly in the task.","Check that the provider is not behind a conditional (cfg, Suspense fallback, route guard) that can render before it exists."],"exampleFix":"// before\n#[component]\nfn App() -> impl IntoView {\n    view! { <Todos/> } // Todos calls query_signal -> panics: no QueryClient context\n}\n\n// after\n#[component]\nfn App() -> impl IntoView {\n    view! {\n        <QueryClientProvider client=QueryClient::new()>\n            <Todos/>\n        </QueryClientProvider>\n    }\n}","handlingStrategy":"fallback","validationCode":"if let Some(client) = use_context::<QueryClient>() { /* proceed */ } else { // render default or log before calling expect_context }","typeGuard":"fn has_context<T: Clone + 'static>() -> bool { use_context::<T>().is_some() }","tryCatchPattern":null,"preventionTips":["Always prefer use_context (Option) over expect_context at component boundaries you do not control.","Place providers at the top of the app tree, above the router.","Verify spawned tasks inherit owners via spawn_local / Owner::with_current."],"tags":["leptos","reactive-graph","context","provider-missing","panic"],"backgroundTag":"context-not-provided","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}