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