Using state in a callback #1058
-
I'm learning Dioxus and having a really hard time figuring out how to use state in an async callback. I have tried using all kinds of combinations of syntax while using fn app(cx: Scope) -> Element {
let content: &UseState<String> = use_state(cx, String::new);
let cb = use_callback!(cx, {
to_owned![content];
move |_| async move {
info!("content: {}", content.current());
}
});
cx.render(rsx! (
div {
textarea{
oninput: |evt| content.set(evt.value.clone()),
"{content}"
}
button{
onmouseup: cb,
"Submit"
}
}
))
} The errors I get are consistently around the content not being able to be moved out of the I've pieced together some stuff from the examples, documentation, and the guide, to no avail. For now, I'm just trying to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
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(){} |
Beta Was this translation helpful? Give feedback.
You don't need to use the use_callback hook here, you can just use the closure directly: