async_web.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //! Basic example that renders a simple VNode to the browser.
  2. use dioxus::events::on::MouseEvent;
  3. use dioxus_core as dioxus;
  4. use dioxus_core::prelude::*;
  5. use dioxus_hooks::*;
  6. use dioxus_html as dioxus_elements;
  7. // use wasm_timer;
  8. use std::future::Future;
  9. use std::{pin::Pin, time::Duration};
  10. use dioxus::prelude::*;
  11. use dioxus_web::*;
  12. fn main() {
  13. // Setup logging
  14. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  15. console_error_panic_hook::set_once();
  16. // Run the app
  17. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  18. }
  19. static App: FC<()> = |cx| {
  20. // let mut count = use_state(cx, || 0);
  21. let state = use_state(cx, || 0);
  22. let set_val = state.setter();
  23. let g = cx.use_task(|| async move {
  24. let mut tick: i32 = 0;
  25. log::debug!("yeet!");
  26. loop {
  27. gloo_timers::future::TimeoutFuture::new(250).await;
  28. log::debug!("ticking forward... {}", tick);
  29. tick += 1;
  30. if tick > 10 {
  31. break;
  32. }
  33. }
  34. set_val(10);
  35. String::from("Huzza!")
  36. });
  37. log::debug!("Value from component was {:#?}", g);
  38. cx.render(rsx! {
  39. div {
  40. section { class: "py-12 px-4 text-center"
  41. div { class: "w-full max-w-2xl mx-auto"
  42. span { class: "text-sm font-semibold"
  43. "count: {state}"
  44. }
  45. div {
  46. button {
  47. onclick: move |_| state.set(state + 1)
  48. "incr"
  49. }
  50. br {}
  51. br {}
  52. button {
  53. onclick: move |_| state.set(state - 1)
  54. "decr"
  55. }
  56. }
  57. }
  58. }
  59. }
  60. })
  61. };