leptos-rs/leptos · error
placeholder parent should be Element
Error message
placeholder parent should be Element
What it means
After finding the parent of the `before` node, mount_before casts it to the renderer's Element type; if the parent node is not an Element (e.g. a DocumentFragment, text node, or shadow root), the cast fails and it panics with 'placeholder parent should be Element'.
Source
Thrown at tachys/src/renderer/dom.rs:231
}
#[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
} else {
falseView on GitHub (pinned to 32d20f6c9d)
Solutions
- Mount relative to an anchor whose parent is a real Element (use an element container)
- Add a wrapper element as parent instead of a raw fragment before mounting
- Use mount/new APIs that insert into an element directly rather than before a fragment-anchored marker
- If writing a custom renderer, override mount_before to handle non-element parents
Example fix
// before
renderer.mount_before(&mut view, &marker_under_fragment); // panics
// after
let holder = document.create_element("div");
fragment_parent.append_child(&holder);
view.mount(&holder, None);
Defensive patterns
Strategy: validation
Validate before calling
fn parent_is_element(node: &web_sys::Node) -> bool {
node.parent_node()
.and_then(|p| p.dyn_into::<web_sys::Element>().ok())
.is_some()
} Type guard
fn parent_as_element(node: &web_sys::Node) -> Option<web_sys::Element> {
node.parent_node()?.dyn_into::<web_sys::Element>().ok()
} Prevention
- Mount relative to anchors whose parent is a real element, not a fragment
- Insert a wrapper <div> between fragments and mounted children
- Use direct-into-element mount APIs when the parent is known
- Handle fragment-rooted views by appending instead of insert-before
When it happens
Trigger: mount_before called where the anchor's parent is a DocumentFragment (e.g. nodes mounted directly under a fragment), a comment/text container, or any non-element parent node returned by get_parent.
Common situations: Mounting views whose root is a fragment into custom containers; using the renderer API on detached fragments; template placeholders placed directly under fragment parents during view reconciliation.
Related errors
- could not find parent element
- event.target not found
- incorrect element type
- tried to remove a parentless node
- Executor::spawn called, but no global 'spawn' function is co
AI-assisted analysis of leptos-rs/leptos@32d20f6c9d (2026-09-01).
Data as JSON: /api/errors/513fc0c4980bfa4f.
Report an issue: GitHub.