Kaynağa Gözat

fix eval example

Jonathan Kelley 2 yıl önce
ebeveyn
işleme
6dfe84834d
1 değiştirilmiş dosya ile 9 ekleme ve 13 silme
  1. 9 13
      examples/eval.rs

+ 9 - 13
examples/eval.rs

@@ -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"
             }