Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the value from state in Yew Rust

I have a state in Yew that looks like this:

let is_flipped: UseStateHandle<bool> = use_state(|| false);

As you can see, its type is UseStateHandle<bool>.

I want to extract its boolean value so I can toggle the state:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let onclick: Callback<MouseEvent> = {
    let is_flipped = is_flipped.clone();
    Callback::from(move |_| match is_flipped {
        true => is_flipped.set(false), // <- type error
        false => is_flipped.set(true), // <- type error
    })
};

But I am getting this error:

^^^^^ expected struct `UseStateHandle`, found `bool`

How can I obtain the boolean value contained in the state?

>Solution :

UseStateHandle implements Deref. You just need to dereference it:

let onclick: Callback<MouseEvent> = {
    let is_flipped = is_flipped.clone();
    Callback::from(move |_| match *is_flipped {
        true => is_flipped.set(false),
        false => is_flipped.set(true),
    })
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading