Skip to content

mark old state as None if unmounting #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/idom/core/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,18 @@ def _render_model_children(
else:
if old_child_state.is_component_state:
self._unmount_model_states([old_child_state])
new_child_state = _update_element_model_state(
old_child_state,
new_state,
index,
)
new_child_state = _make_element_model_state(
new_state,
index,
key,
)
old_child_state = None
else:
new_child_state = _update_element_model_state(
old_child_state,
new_state,
index,
)
self._render_model(old_child_state, new_child_state, child)
new_children.append(new_child_state.model.current)
new_state.children_by_key[key] = new_child_state
Expand Down
39 changes: 39 additions & 0 deletions tests/test_core/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import idom
from idom import html
from idom.config import IDOM_DEBUG_MODE
from idom.core.dispatcher import render_json_patch
from idom.core.hooks import use_effect
Expand Down Expand Up @@ -838,3 +839,41 @@ def HasState():
root_hook.latest.schedule_render()
await layout.render()
assert last_state != state.current


async def test_switching_node_type_with_event_handlers():
toggle_type = idom.Ref()
element_static_handler = StaticEventHandler()
component_static_handler = StaticEventHandler()

@idom.component
def Root():
toggle, toggle_type.current = use_toggle(True)
handler = element_static_handler.use(lambda: None)
if toggle:
return html.div(html.button({"onEvent": handler}))
else:
return html.div(SomeComponent())

@idom.component
def SomeComponent():
handler = component_static_handler.use(lambda: None)
return html.button({"onAnotherEvent": handler})

with idom.Layout(Root()) as layout:
await layout.render()

assert element_static_handler.target in layout._event_handlers
assert component_static_handler.target not in layout._event_handlers

toggle_type.current()
await layout.render()

assert element_static_handler.target not in layout._event_handlers
assert component_static_handler.target in layout._event_handlers

toggle_type.current()
await layout.render()

assert element_static_handler.target in layout._event_handlers
assert component_static_handler.target not in layout._event_handlers