Skip to content

Commit 250a884

Browse files
nikhiljhanikhil-cognition
authored andcommitted
fix(native-dom): clear node_id_mapping when nodes are removed
When `remove_node` or `replace_node_with` removes a node from the DOM, the corresponding entry in `node_id_mapping` was not being cleared. This left stale ElementId → NodeId mappings. Since dioxus-core recycles ElementIds aggressively, a recycled ElementId could find a stale mapping in `assign_node_id`, which would then call `remove_node_if_unparented` on either: - An already-removed node (causing panic in blitz-dom: "invalid key") - A node still on the mutation stack waiting to be attached (corrupting the DOM by removing nodes that should be inserted) This fix clears the mapping immediately when nodes are removed, ensuring the invariant: if `node_id_mapping[eid] = Some(nid)`, then `nid` is valid in blitz-dom.
1 parent ec8f31d commit 250a884

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

packages/native-dom/src/mutation_writer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ impl WriteMutations for MutationWriter<'_> {
144144
fn replace_node_with(&mut self, id: ElementId, m: usize) {
145145
trace!("replace_node_with id:{} m:{}", id.0, m);
146146
let (anchor_node_id, new_node_ids) = self.state.anchor_and_nodes(id, m);
147+
if let Some(mapping) = self.state.node_id_mapping.get_mut(id.0) {
148+
*mapping = None;
149+
}
147150
self.docm.replace_node_with(anchor_node_id, &new_node_ids);
148151
}
149152

@@ -160,6 +163,9 @@ impl WriteMutations for MutationWriter<'_> {
160163
fn remove_node(&mut self, id: ElementId) {
161164
trace!("remove_node id:{}", id.0);
162165
let node_id = self.state.element_to_node_id(id);
166+
if let Some(mapping) = self.state.node_id_mapping.get_mut(id.0) {
167+
*mapping = None;
168+
}
163169
self.docm.remove_node(node_id);
164170
}
165171

0 commit comments

Comments
 (0)