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

Reading a resource breaks reactivity #2225

Closed
thorn132 opened this issue Apr 2, 2024 · 1 comment · Fixed by #2236
Closed

Reading a resource breaks reactivity #2225

thorn132 opened this issue Apr 2, 2024 · 1 comment · Fixed by #2236
Assignees
Labels
bug Something isn't working hooks Changes to built-in hook package
Milestone

Comments

@thorn132
Copy link

thorn132 commented Apr 2, 2024

Problem

Reading a resource (e.g. by res.read(), res.value(), or res.state()) breaks reactivity on the component that performed the read.

Steps To Reproduce

#[component]
fn App() -> Element {
    let mut x = use_signal(|| 0);

    let res = use_resource(move || {
        println!("resource triggered {}", x());
        async move { x() }
    });

    use_effect(move || println!("effect triggered {}", x()));

    // println!("resource state {:?}", res.state());
    // println!("resource value {:?}", res.value());
    // println!("resource read {:?}", res.read());

    rsx! {
        button {
            onclick: move |_| x += 1,
            "Trigger"
        }
    }
}

Un-comment any of the uses of res to enable the bug.

Expected behavior

When res is not used, the console output with N button clicks will be:

resource triggered 0
effect triggered 0
resource triggered 1
effect triggered 1
...
resource triggered N
effect triggered N

When all uses of res are un-commented, it is instead:

resource triggered 0
resource state Pending
resource value None
resource read None
resource state Ready
resource value Some(0)
resource read Some(0)

where clicking the button has no effect.

The use_effect on the signal x still breaks even if res does not use x.

Environment:

  • Dioxus version: 0.5.0 and main 46b0eeb
  • Rust version: 1.76 stable
  • OS info: Windows 11
  • App platform: desktop
@ealmloff
Copy link
Member

ealmloff commented Apr 2, 2024

It seems like the duration of the future matters here. This code works as expected:

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

    let res = use_resource(move || {
        println!("resource triggered {}", x());
        async move {
            tokio::time::sleep(std::time::Duration::from_millis(1)).await;
            x()
        }
    });

    use_effect(move || println!("effect triggered {}", x()));

    println!("resource state {:?}", res.state());
    println!("resource value {:?}", res.value());
    println!("resource read {:?}", res.read());

    rsx! {
        button {
            onclick: move |_| x += 1,
            "Trigger"
        }
    }
}

@ealmloff ealmloff added bug Something isn't working hooks Changes to built-in hook package labels Apr 2, 2024
@jkelleyrtp jkelleyrtp added this to the 0.6.0: Devtools milestone Apr 2, 2024
@jkelleyrtp jkelleyrtp self-assigned this Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hooks Changes to built-in hook package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants