@@ -234,15 +234,22 @@ def _render_model(
234234 raw_model : Any ,
235235 ) -> None :
236236 new_state .model .current = {"tagName" : raw_model ["tagName" ]}
237-
238- self ._render_model_attributes (old_state , new_state , raw_model )
239- self ._render_model_children (old_state , new_state , raw_model .get ("children" , []))
240-
241237 if "key" in raw_model :
242- new_state .model .current ["key" ] = raw_model ["key" ]
238+ new_state .key = new_state . model .current ["key" ] = raw_model ["key" ]
243239 if "importSource" in raw_model :
244240 new_state .model .current ["importSource" ] = raw_model ["importSource" ]
245241
242+ if old_state is not None and old_state .key != new_state .key :
243+ self ._unmount_model_states ([old_state ])
244+ if new_state .is_component_state :
245+ self ._model_states_by_life_cycle_state_id [
246+ new_state .life_cycle_state .id
247+ ] = new_state
248+ old_state = None
249+
250+ self ._render_model_attributes (old_state , new_state , raw_model )
251+ self ._render_model_children (old_state , new_state , raw_model .get ("children" , []))
252+
246253 def _render_model_attributes (
247254 self ,
248255 old_state : Optional [_ModelState ],
@@ -604,6 +611,9 @@ def parent(self) -> _ModelState:
604611 assert parent is not None , "detached model state"
605612 return parent
606613
614+ def __repr__ (self ) -> str :
615+ return f"ModelState({ {s : getattr (self , s , None ) for s in self .__slots__ } } )"
616+
607617
608618def _make_life_cycle_state (
609619 component : ComponentType ,
0 commit comments