fix eval example

This commit is contained in:
Jonathan Kelley 2022-12-30 02:06:33 -05:00
parent d160a5c8ff
commit 6dfe84834d
1 changed files with 9 additions and 13 deletions

View File

@ -1,25 +1,17 @@
use dioxus::prelude::*;
use dioxus_desktop::EvalResult;
fn main() {
dioxus_desktop::launch(app);
}
fn app(cx: Scope) -> Element {
let script = use_state(cx, String::new);
let eval = dioxus_desktop::use_eval(cx);
let future: &UseRef<Option<EvalResult>> = use_ref(cx, || None);
if future.read().is_some() {
let future_clone = future.clone();
cx.spawn(async move {
if let Some(fut) = future_clone.with_mut(|o| o.take()) {
println!("{:?}", fut.await)
}
});
}
let script = use_state(cx, String::new);
let output = use_state(cx, String::new);
cx.render(rsx! {
div {
p { "Output: {output}" }
input {
placeholder: "Enter an expression",
value: "{script}",
@ -27,8 +19,12 @@ fn app(cx: Scope) -> Element {
}
button {
onclick: move |_| {
let fut = eval(script);
future.set(Some(fut));
to_owned![script, eval, output];
cx.spawn(async move {
if let Ok(res) = eval(script.to_string()).await {
output.set(res.to_string());
}
});
},
"Execute"
}