async.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! Example: README.md showcase
  2. //!
  3. //! The example from the README.md
  4. use std::pin::Pin;
  5. use dioxus::prelude::*;
  6. use futures::Future;
  7. fn main() {
  8. env_logger::init();
  9. log::info!("hello world");
  10. dioxus::desktop::launch(App, |c| c).expect("faield to launch");
  11. }
  12. #[derive(serde::Deserialize)]
  13. struct DogApi {
  14. message: String,
  15. }
  16. const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
  17. struct Ex(Pin<Box<dyn Future<Output = ()> + 'static>>);
  18. static App: FC<()> = |cx| {
  19. // let mut count = use_state(cx, || 0);
  20. let mut fut = cx.use_hook(
  21. move || {
  22. Ex(Box::pin(async {
  23. //
  24. loop {
  25. match surf::get(ENDPOINT).recv_json::<DogApi>().await {
  26. Ok(_) => (),
  27. Err(_) => (),
  28. }
  29. }
  30. })
  31. as Pin<Box<dyn Future<Output = ()> + 'static>>)
  32. },
  33. |h| &mut h.0,
  34. |_| {},
  35. );
  36. cx.submit_task(fut);
  37. cx.render(rsx! {
  38. div {
  39. }
  40. })
  41. };