{"record":{"id":"5228ceb8317b95f1","repo":"yewstack/yew","slug":"use-linked-state-requires-a-linkprovider","errorCode":null,"errorMessage":"use_linked_state requires a LinkProvider","messagePattern":"use_linked_state requires a LinkProvider","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"packages/yew-link/src/lib.rs","lineNumber":512,"sourceCode":"/// state is fetched from the provider's `endpoint` URL.\n///\n/// The hook suspends while the state is being resolved/fetched for the first\n/// time. On [`refresh`](LinkedStateHandle::refresh), the previous value is\n/// kept visible (stale-while-revalidate) and\n/// [`is_refreshing`](LinkedStateHandle::is_refreshing) returns `true` until\n/// the fresh data arrives.\n///\n/// Multiple components requesting the same `(T, Input)` concurrently share a\n/// single in-flight request.\n///\n/// # Panics\n///\n/// Panics if there is no ancestor [`LinkProvider`] in the component tree.\n#[hook]\npub fn use_linked_state<T: LinkedState>(input: T::Input) -> SuspensionResult<LinkedStateHandle<T>> {\n    #[cfg(any(feature = \"ssr\", target_arch = \"wasm32\"))]\n    let link_ctx =\n        use_context::<LinkContextInner>().expect(\"use_linked_state requires a LinkProvider\");\n\n    #[cfg(any(feature = \"ssr\", target_arch = \"wasm32\"))]\n    type Prepared<T, E> = Result<T, LinkError<E>>;\n\n    #[cfg(feature = \"ssr\")]\n    {\n        let prepared = {\n            let link_ctx = link_ctx.clone();\n            yew::functional::use_prepared_state_with_suspension(\n                input,\n                move |input: Rc<T::Input>| {\n                    let link_ctx = link_ctx.clone();\n                    async move { link_ctx.resolve_local::<T>(&input).await }\n                },\n            )\n        }?;\n\n        let result: Rc<Prepared<T, T::Error>> =","sourceCodeStart":494,"sourceCodeEnd":530,"githubUrl":"https://github.com/yewstack/yew/blob/0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3/packages/yew-link/src/lib.rs#L494-L530","documentation":"use_linked_state<T> (packages/yew-link/src/lib.rs:512) reads LinkContextInner via use_context and panics with .expect when no ancestor rendered <LinkProvider>. The hook needs the provider's cache, endpoint and (on SSR) resolver to fetch or resolve linked state, so a missing provider is always a programming error, not a recoverable condition — the doc comment explicitly states it panics.","triggerScenarios":"Calling use_linked_state in a component mounted outside a <LinkProvider> subtree; putting the provider next to instead of above the consuming component; rendering the hook during SSR with no provider at all in the server tree.","commonSituations":"Adding a linked-state hook to a new page while the provider only wraps other routes; reorganizing the app layout so the root provider is dropped; testing a component in isolation without wrapping it in a provider.","solutions":["Mount <LinkProvider> above every component that calls use_linked_state — app root is the safest place: html! { <LinkProvider endpoint={AttrValue::from(\"/api/link\")}> <App /> </LinkProvider> }","In tests, wrap the component under test with a minimal LinkProvider","On SSR additionally pass a resolver prop (see the resolve_local panic) so the provider is fully configured"],"exampleFix":"// before\nhtml! { <ProfilePage /> }  // ProfilePage calls use_linked_state\n\n// after\nhtml! {\n    <LinkProvider endpoint={AttrValue::from(\"/api/link\")}>\n        <ProfilePage />\n    </LinkProvider>\n}","handlingStrategy":"validation","validationCode":"// render every use_linked_state consumer inside a LinkProvider\n// html! { <LinkProvider endpoint={AttrValue::from(\"/api/link\")}> <App /> </LinkProvider> }","typeGuard":"// debug-only check you can drop into a component before calling the hook\n// #[cfg(debug_assertions)]\n// if yew::functional::use_context::<crate::LinkContextInner>().is_none() {\n//     panic!(\"use_linked_state requires a LinkProvider ancestor\");\n// }","tryCatchPattern":null,"preventionTips":["Place LinkProvider at the root layout so all routes inherit it","When extracting a component into a test or story, wrap it in a minimal LinkProvider","Treat 'no provider' as a wiring bug, not a runtime condition to recover from"],"tags":["rust","wasm","yew-link","context-provider","runtime-panic"],"backgroundTag":"missing-context-provider","analyzedSha":"0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3","analyzedAt":"2026-08-22T21:16:31.212Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}