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

Value not updating with UseState == #619

Closed
ealmloff opened this issue Nov 13, 2022 · 1 comment · Fixed by #1791
Closed

Value not updating with UseState == #619

ealmloff opened this issue Nov 13, 2022 · 1 comment · Fixed by #1791

Comments

@ealmloff
Copy link
Member

ealmloff commented Nov 13, 2022

Problem

use dioxus::prelude::*;

fn main() {
    dioxus_web::launch(app);
}

fn app(cx: Scope) -> Element {
    let mut count = use_state(&cx, || 0);
    count += 1;
    if count == 0 {
        panic!("count should be 1");
    }

    cx.render(rsx! {
        div{
            "{count}"
        }
    })
}

Expected behavior

The count should be 1 and the program should not panic.

Environment:

  • Dioxus version: tested on master and 0.2.4
  • Rust version: 1.65.0
  • OS info: Windows
  • App platform: All
@ealmloff ealmloff changed the title Unexpected Behavior with UseState PartialEq Unexpected Behavior with UseState get Nov 13, 2022
@ealmloff ealmloff changed the title Unexpected Behavior with UseState get Value not updating with UseState == Nov 13, 2022
@ealmloff
Copy link
Member Author

ealmloff commented Dec 9, 2022

I've been thinking about this more. I don't think there is any way to resolve this problem while keeping the Deref behavior and not panicking if the value is currently mutably borrowed on PartialEq, Add, etc implementations which is the main difference between UseState and UseRef...

We either need to give up that behavior and unify UseState and UseRef or keep the behavior and leave odd updating behavior as a footgun.

If we keep the behavior we need to update the documentation to state that the value is only valid before writes instead of only warning it is invalid in futures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant