async_web.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #[derive(serde::Deserialize)]
  20. struct DogApi {
  21. message: String,
  22. }
  23. const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random/";
  24. static App: FC<()> = |cx| {
  25. // let mut count = use_state(cx, || 0);
  26. let state = use_state(cx, || 0);
  27. let set_val = state.setter();
  28. let g = cx.use_task(|| async move {
  29. let mut tick: i32 = 0;
  30. log::debug!("yeet!");
  31. // loop {
  32. // gloo_timers::future::TimeoutFuture::new(250).await;
  33. // log::debug!("ticking forward... {}", tick);
  34. // tick += 1;
  35. // if tick > 10 {
  36. // break;
  37. // }
  38. // }
  39. set_val(10);
  40. surf::get(ENDPOINT).recv_json::<DogApi>().await
  41. // String::from("Huzza!")
  42. });
  43. let dog_node = match g.as_ref().and_then(|f| f.as_ref().ok()) {
  44. Some(res) => rsx!(in cx, img { src: "{res.message}" }),
  45. None => rsx!(in cx, div { "No doggos for you :(" }),
  46. };
  47. cx.render(rsx! {
  48. div {
  49. section { class: "py-12 px-4 text-center"
  50. div { class: "w-full max-w-2xl mx-auto"
  51. span { class: "text-sm font-semibold"
  52. "count: {state}"
  53. }
  54. div {
  55. button {
  56. onclick: move |_| state.set(state + 1)
  57. "incr"
  58. }
  59. br {}
  60. br {}
  61. button {
  62. onclick: move |_| state.set(state - 1)
  63. "decr"
  64. }
  65. }
  66. div {
  67. h1{"doggo!"}
  68. {dog_node}
  69. }
  70. }
  71. }
  72. }
  73. })
  74. };