Skip to content
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
13 changes: 13 additions & 0 deletions packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,19 @@ mod feat_csr {
}

pub(crate) fn reuse(&self, props: Rc<COMP::Properties>, slot: DomSlot) {
if let Some(state) = self.state.borrow_mut().as_mut() {
match &state.render_state {
ComponentRenderState::Render { sibling_slot, .. } => {
sibling_slot.reassign(slot.clone());
}
#[cfg(feature = "hydration")]
ComponentRenderState::Hydration { sibling_slot, .. } => {
sibling_slot.reassign(slot.clone());
}
#[cfg(feature = "ssr")]
ComponentRenderState::Ssr { .. } => {}
}
}
schedule_props_update(self.state.clone(), props, slot)
}
}
Expand Down
57 changes: 57 additions & 0 deletions packages/yew/tests/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,60 @@ async fn use_state_handle_as_prop_triggers_child_rerender_issue_4058() {
CHILD_RENDER_COUNT.load(Ordering::Relaxed)
);
}

#[wasm_bindgen_test]
async fn toggle_conditional_with_empty_component_no_crash() {
use wasm_bindgen::JsCast;
use web_sys::HtmlElement;

#[component]
fn Empty() -> Html {
html! {}
}

#[component]
fn App() -> Html {
let toggled = use_state(|| false);

let onclick = {
let toggled = toggled.clone();
Callback::from(move |_: MouseEvent| {
toggled.set(!*toggled);
})
};

html! {
<>
if *toggled {
<span></span>
}
<Empty />
if !*toggled { <div>{"old"}</div> }
<button id="toggle-btn" {onclick}>{"Toggle"}</button>
<div id="result">{ if *toggled { "toggled" } else { "initial" } }</div>
</>
}
}

yew::Renderer::<App>::with_root(gloo::utils::document().get_element_by_id("output").unwrap())
.render();
scheduler::flush().await;

let result = obtain_result();
assert_eq!(result.as_str(), "initial");

gloo::utils::document()
.get_element_by_id("toggle-btn")
.unwrap()
.unchecked_into::<HtmlElement>()
.click();

scheduler::flush().await;

let result = obtain_result();
assert_eq!(
result.as_str(),
"toggled",
"Toggling conditional blocks with empty components must not crash (issue #4092)"
);
}
Loading