@@ -271,10 +271,10 @@ def _render_model_attributes(
271271
272272 model_event_handlers = new_state .model .current ["eventHandlers" ] = {}
273273 for event , handler in handlers_by_event .items ():
274- target = old_state .targets_by_event . get (
275- event ,
276- uuid4 (). hex if handler . target is None else handler . target ,
277- )
274+ if event in old_state .targets_by_event :
275+ target = old_state . targets_by_event [ event ]
276+ else :
277+ target = uuid4 (). hex if handler . target is None else handler . target
278278 new_state .targets_by_event [event ] = target
279279 self ._event_handlers [target ] = handler
280280 model_event_handlers [event ] = {
@@ -350,6 +350,8 @@ def _render_model_children(
350350 key ,
351351 )
352352 else :
353+ if hasattr (old_child_state , "life_cycle_hook" ):
354+ old_child_state .life_cycle_state .hook .component_will_unmount ()
353355 new_child_state = _update_element_model_state (
354356 old_child_state ,
355357 new_state ,
@@ -374,6 +376,7 @@ def _render_model_children(
374376 new_state ,
375377 index ,
376378 child ,
379+ self ._rendering_queue .put ,
377380 )
378381 self ._render_component (old_child_state , new_child_state , child )
379382 else :
@@ -459,7 +462,6 @@ def _make_component_model_state(
459462
460463
461464def _copy_component_model_state (old_model_state : _ModelState ) -> _ModelState :
462-
463465 # use try/except here because not having a parent is rare (only the root state)
464466 try :
465467 parent : Optional [_ModelState ] = old_model_state .parent
@@ -483,15 +485,8 @@ def _update_component_model_state(
483485 new_parent : _ModelState ,
484486 new_index : int ,
485487 new_component : ComponentType ,
488+ schedule_render : Callable [[_LifeCycleStateId ], None ],
486489) -> _ModelState :
487- try :
488- old_life_cycle_state = old_model_state .life_cycle_state
489- except AttributeError :
490- raise ValueError (
491- f"Failed to render layout at { old_model_state .patch_path !r} with key "
492- f"{ old_model_state .key !r} - prior element with this key wasn't a component"
493- )
494-
495490 return _ModelState (
496491 parent = new_parent ,
497492 index = new_index ,
@@ -500,7 +495,11 @@ def _update_component_model_state(
500495 patch_path = old_model_state .patch_path ,
501496 children_by_key = {},
502497 targets_by_event = {},
503- life_cycle_state = _update_life_cycle_state (old_life_cycle_state , new_component ),
498+ life_cycle_state = (
499+ old_model_state .life_cycle_state
500+ if hasattr (old_model_state , "life_cycle_hook" )
501+ else _make_life_cycle_state (new_component , schedule_render )
502+ ),
504503 )
505504
506505
@@ -525,12 +524,6 @@ def _update_element_model_state(
525524 new_parent : _ModelState ,
526525 new_index : int ,
527526) -> _ModelState :
528- if hasattr (old_model_state , "life_cycle_state" ):
529- raise ValueError (
530- f"Failed to render layout at { old_model_state .patch_path !r} with key "
531- f"{ old_model_state .key !r} - prior element with this key was a component"
532- )
533-
534527 return _ModelState (
535528 parent = new_parent ,
536529 index = new_index ,
0 commit comments