diff --git a/tachys/src/html/element/mod.rs b/tachys/src/html/element/mod.rs
index 03ad87bb6..9f28146a7 100644
--- a/tachys/src/html/element/mod.rs
+++ b/tachys/src/html/element/mod.rs
@@ -317,6 +317,26 @@ where
type State = ElementState;
fn rebuild(self, state: &mut Self::State) {
+ // check whether the tag is the same, for custom elements
+ // because this is const `false` for all other element types,
+ // the compiler should be able to optimize it out
+ if E::TAG.is_empty() {
+ // see https://github.com/leptos-rs/leptos/issues/4412
+ let new_tag = self.tag.tag();
+
+ // this is not particularly efficient, but it saves us from
+ // having to keep track of the tag name for every element state
+ let old_tag = state.el.tag_name();
+ if new_tag != old_tag {
+ let mut new_state = self.build();
+ state.insert_before_this(&mut new_state);
+ state.unmount();
+ *state = new_state;
+ return;
+ }
+ }
+
+ // rebuild attributes and children for any element
let ElementState {
attrs, children, ..
} = state;