DioxusLabs / dioxus

Fullstack GUI library for web, desktop, mobile, and more.
https://dioxuslabs.com
Apache License 2.0
19.33k stars 736 forks source link

bug: `spawn_forever` tasks get attached to the scope #2215

Closed marc2332 closed 3 months ago

marc2332 commented 3 months ago

Problem spawn_forever tasks get attached to the scope

The task will never get executed because it's attached to the Scope, and the Scope is being dropped.

#[allow(non_snake_case)]
fn Counter() -> Element {
    use_drop(move || {
        println!("spawning");
        spawn_forever(async move {
            println!("running");
        });
    });

    None
}

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

    rsx!(
        Button {
            onclick: move |_| count += 1,
            label { "Increase" }
        }
        Button {
            onclick: move |_| count -= 1,
            label { "Decrease" }
        }
        for i in 0..count() {
            Counter {
                key: "{i}"
            }
        }
    )
}

Expected behavior

Work

Environment: