{"record":{"id":"5a9311975248de6f","repo":"leptos-rs/leptos","slug":"could-not-find-parent-element","errorCode":null,"errorMessage":"could not find parent element","messagePattern":"could not find parent element","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tachys/src/renderer/dom.rs","lineNumber":229,"sourceCode":"    pub fn log_node(node: &Node) {\n        web_sys::console::log_1(node);\n    }\n\n    #[cfg_attr(feature = \"tracing\", tracing::instrument(level = \"trace\"))]\n    pub fn clear_children(parent: &Element) {\n        parent.set_text_content(Some(\"\"));\n    }\n\n    /// Mounts the new child before the marker as its sibling.\n    ///\n    /// ## Panics\n    /// The default implementation panics if `before` does not have a parent [`crate::renderer::types::Element`].\n    pub fn mount_before<M>(new_child: &mut M, before: &Node)\n    where\n        M: Mountable,\n    {\n        let parent = Element::cast_from(\n            Self::get_parent(before).expect(\"could not find parent element\"),\n        )\n        .expect(\"placeholder parent should be Element\");\n        new_child.mount(&parent, Some(before));\n    }\n\n    /// Tries to mount the new child before the marker as its sibling.\n    ///\n    /// Returns `false` if the child did not have a valid parent.\n    #[track_caller]\n    pub fn try_mount_before<M>(new_child: &mut M, before: &Node) -> bool\n    where\n        M: Mountable,\n    {\n        if let Some(parent) =\n            Self::get_parent(before).and_then(Element::cast_from)\n        {\n            new_child.mount(&parent, Some(before));\n            true","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/leptos-rs/leptos/blob/32d20f6c9d517451d77beb68d88c7716823dac41/tachys/src/renderer/dom.rs#L211-L247","documentation":"DomRenderer::mount_before locates the parent of the `before` marker node via get_parent and panics if there is none, because a node can only be inserted relative to an existing parent. The doc comment explicitly states the default implementation panics in this case.","triggerScenarios":"Calling mount_before with a `before` node that has been detached from the DOM, a node that was never mounted, or mounting into a fragment/marker whose parent was removed during reconciliation.","commonSituations":"Manual DOM manipulation removing an element that Leptos still references as a mount anchor; conditional rendering logic unmounting a subtree while another branch still mounts relative to it; hydration mismatches removing marker nodes.","solutions":["Ensure the `before` node is still attached and mounted when mount_before is called","Keep marker nodes intact — don't remove Leptos-managed DOM nodes manually","Re-mount the parent container before inserting relative to it","If writing custom renderer code, check get_parent/before.parentNode first and fall back to mount on the container"],"exampleFix":"// before\nrenderer.mount_before(&mut child, &detached_marker); // panics\n// after\nif !detached_marker.is_connected() { child.mount(&container, None); }\nelse { renderer.mount_before(&mut child, &detached_marker); }\n","handlingStrategy":"validation","validationCode":"// wasm/web-sys\nfn has_parent(node: &web_sys::Node) -> bool { node.parent_node().is_some() }\n// call before: if !has_parent(before_marker) { /* mount into container instead */ }","typeGuard":null,"tryCatchPattern":"// validate connectivity first; only mount_before when parent exists:\nif before.parent_node().is_some() {\n    renderer.mount_before(&mut child, before);\n} else {\n    child.mount(&container, None);\n}","preventionTips":["Don't remove Leptos-managed DOM nodes or markers manually","Verify anchors are still in the document before relative mounts","Avoid structural DOM mutations that race with view mounting","In tests, assert marker.isConnected before mount_before"],"tags":["dom","mounting","panic"],"backgroundTag":"dom-node-not-attached","analyzedSha":"32d20f6c9d517451d77beb68d88c7716823dac41","analyzedAt":"2026-09-01T18:55:42.580Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}