async_web.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 fut = cx.use_hook(
  22. move || {
  23. Box::pin(async {
  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. }
  31. }) as Pin<Box<dyn Future<Output = ()> + 'static>>
  32. },
  33. |h| h,
  34. |_| {},
  35. );
  36. cx.submit_task(fut);
  37. let state = use_state(cx, || 0);
  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. };