leptos-rs/leptos · error

could not find parent element

Error message

could not find parent element

What it means

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.

Source

Thrown at tachys/src/renderer/dom.rs:229

    pub fn log_node(node: &Node) {
        web_sys::console::log_1(node);
    }

    #[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
    pub fn clear_children(parent: &Element) {
        parent.set_text_content(Some(""));
    }

    /// Mounts the new child before the marker as its sibling.
    ///
    /// ## Panics
    /// The default implementation panics if `before` does not have a parent [`crate::renderer::types::Element`].
    pub fn mount_before<M>(new_child: &mut M, before: &Node)
    where
        M: Mountable,
    {
        let parent = Element::cast_from(
            Self::get_parent(before).expect("could not find parent element"),
        )
        .expect("placeholder parent should be Element");
        new_child.mount(&parent, Some(before));
    }

    /// Tries to mount the new child before the marker as its sibling.
    ///
    /// Returns `false` if the child did not have a valid parent.
    #[track_caller]
    pub fn try_mount_before<M>(new_child: &mut M, before: &Node) -> bool
    where
        M: Mountable,
    {
        if let Some(parent) =
            Self::get_parent(before).and_then(Element::cast_from)
        {
            new_child.mount(&parent, Some(before));
            true

View on GitHub (pinned to 32d20f6c9d)

Solutions

  1. Ensure the `before` node is still attached and mounted when mount_before is called
  2. Keep marker nodes intact — don't remove Leptos-managed DOM nodes manually
  3. Re-mount the parent container before inserting relative to it
  4. If writing custom renderer code, check get_parent/before.parentNode first and fall back to mount on the container

Example fix

// before
renderer.mount_before(&mut child, &detached_marker); // panics
// after
if !detached_marker.is_connected() { child.mount(&container, None); }
else { renderer.mount_before(&mut child, &detached_marker); }
Defensive patterns

Strategy: validation

Validate before calling

// wasm/web-sys
fn has_parent(node: &web_sys::Node) -> bool { node.parent_node().is_some() }
// call before: if !has_parent(before_marker) { /* mount into container instead */ }

Try / catch

// validate connectivity first; only mount_before when parent exists:
if before.parent_node().is_some() {
    renderer.mount_before(&mut child, before);
} else {
    child.mount(&container, None);
}

Prevention

When it happens

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

Common situations: 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.

Related errors


AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01). Data as JSON: /api/errors/5a9311975248de6f. Report an issue: GitHub.