Skip to content
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

Rerender fails when rendering an iterable with None elements inside #2393

Closed
ochrons opened this issue May 5, 2024 · 1 comment · Fixed by #2749
Closed

Rerender fails when rendering an iterable with None elements inside #2393

ochrons opened this issue May 5, 2024 · 1 comment · Fixed by #2749
Assignees
Labels
bug Something isn't working core relating to the core implementation of the virtualdom
Milestone

Comments

@ochrons
Copy link

ochrons commented May 5, 2024

Problem

When rendering a list of components again with a different list, where the first list contains some None (ie. empty nodes), there is a panic:

panicked at /home/ochrons/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-core-0.5.1/src/diff/node.rs:142:49:
called `Option::unwrap()` on a `None` value

$core::option::unwrap_failed::hb5bacfb0dd292085    @    Zhef_bg.wasm:0x513587
$dioxus_core::diff::node::<impl dioxus_core::nodes::VNode>::find_last_element::h2fbafeb25fa6485a    @    Zhef_bg.wasm:0x24a677
$dioxus_core::diff::iterator::<impl dioxus_core::virtual_dom::VirtualDom>::create_and_insert_after::hdf1b487b5c58bd2e    @    Zhef_bg.wasm:0x48288f
$dioxus_core::diff::iterator::<impl dioxus_core::virtual_dom::VirtualDom>::diff_non_keyed_children::h825ddb6722191889    @    Zhef_bg.wasm:0x236220
$dioxus_core::diff::iterator::<impl dioxus_core::virtual_dom::VirtualDom>::diff_non_empty_fragment::h9f386364ef278952    @    Zhef_bg.wasm:0x2588a3
$dioxus_core::diff::node::<impl dioxus_core::nodes::VNode>::diff_dynamic_node::hccca532b469dca0c    @    Zhef_bg.wasm:0x19bbcf
$dioxus_core::diff::node::<impl dioxus_core::nodes::VNode>::diff_node::{{closure}}::he12ecb157d0a2bc4    @    Zhef_bg.wasm:0x4e38

I'm rendering a li with changing number of children

let description: impl Iterator<Item = Option<VNode>>
...
rsx! {
    li { {description} }
}

some of the items in the iterator are None. The generated HTML looks like

<li data-node-hydration="103"><!--node-id104-->Shape <!--#--><!--node-id105--> into <!--#--></li>

if I replace the empty nodes with some valid rsx (empty string), this problem doesn't occur and then the HTML looks like

<li data-node-hydration="118">
<!--node-id119-->Shape <!--#--><!--node-id120--><!--node-id121--> into <!--#--><!--node-id122--><!--node-id123--><!--#-->
</li>

Expected behavior

Shouldn't panic, but render the new list of items correctly.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment:

  • Dioxus version: 0.5.1
  • Rust version: 1.78
  • OS info: Win11 WSL2
  • App platform: fullstack
@ealmloff ealmloff added the bug Something isn't working label Jul 24, 2024
@jkelleyrtp jkelleyrtp added this to the 0.6.0: Fullstack milestone Jul 25, 2024
@jkelleyrtp jkelleyrtp added the core relating to the core implementation of the virtualdom label Jul 30, 2024
@ealmloff
Copy link
Member

Reproduction with the latest version of dioxus main:

use dioxus::prelude::*;

fn main() {
    launch(app);
}

fn app() -> Element {
    let mut count = use_signal(|| 0);

    let items = (0..count()).map(|_| {
        if count() % 2 == 0 {
            VNode::empty()
        } else {
            rsx! {
                li {
                    "Fizz"
                }
            }
        }
    });

    rsx! {
        h1 { "High-Five counter: {count}" }
        button { onclick: move |_| count += 1, "Up high!" }
        button { onclick: move |_| count -= 1, "Down low!" }
        ul {
            {items}
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core relating to the core implementation of the virtualdom
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants