Skip to content

Using state in a callback #1058

Answered by ealmloff
w3irdrobot asked this question in Q&A
May 30, 2023 · 1 comments · 10 replies
Discussion options

You must be logged in to vote

You don't need to use the use_callback hook here, you can just use the closure directly:

use dioxus::prelude::*;

fn app(cx: Scope) -> Element {
    let content: &UseState<String> = use_state(cx, String::new);
    let cb = move |_| {
        to_owned![content];
        async move {
            println!("content: {}", content.current());
        }
    };

    cx.render(rsx! (
        div {
            textarea{
                oninput: |evt| content.set(evt.value.clone()),
                "{content}"
            }
            button{
                onmouseup: cb,
                "Submit"
            }
        }
    ))
}

fn main(){}

Replies: 1 comment 10 replies

Comment options

You must be logged in to vote
10 replies
@w3irdrobot
Comment options

@ealmloff
Comment options

@w3irdrobot
Comment options

@ealmloff
Comment options

@w3irdrobot
Comment options

Answer selected by w3irdrobot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants