dioxus/packages/web/examples/async_web.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

//! Basic example that renders a simple VNode to the browser.
use dioxus::events::on::MouseEvent;
use dioxus_core as dioxus;
use dioxus_core::prelude::*;
use dioxus_hooks::*;
use dioxus_html as dioxus_elements;
// use wasm_timer;
use std::future::Future;
use std::{pin::Pin, time::Duration};
use dioxus::prelude::*;
fn main() {
// Setup logging
console_error_panic_hook::set_once();
2021-07-18 07:54:42 +00:00
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
2021-07-18 07:54:42 +00:00
dioxus_web::launch(App, |c| c)
}
2021-07-14 06:30:41 +00:00
#[derive(serde::Deserialize)]
struct DogApi {
message: String,
}
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random/";
static App: FC<()> = |cx| {
let state = use_state(cx, || 0);
2021-07-15 04:40:37 +00:00
let dog_node = cx.use_suspense(
|| surf::get(ENDPOINT).recv_json::<DogApi>(),
|cx, res| match res {
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
2021-07-18 07:54:42 +00:00
Err(_err) => rsx!(in cx, div { "No doggos for you :(" }),
2021-07-15 04:40:37 +00:00
},
);
cx.render(rsx! {
2021-07-18 07:54:42 +00:00
div { style: { align_items: "center" }
section { class: "py-12 px-4 text-center"
div { class: "w-full max-w-2xl mx-auto"
span { class: "text-sm font-semibold"
"count: {state}"
}
2021-07-15 03:18:02 +00:00
br {}
div {
button {
onclick: move |_| state.set(state + 1)
"incr"
}
br {}
button {
onclick: move |_| state.set(state - 1)
"decr"
}
}
2021-07-14 06:30:41 +00:00
div {
h1{"doggo!"}
{dog_node}
}
}
}
}
})
};