dioxus/packages/web/examples/hydrate.rs

58 lines
1.2 KiB
Rust
Raw Normal View History

2022-06-25 13:27:10 +00:00
use dioxus::prelude::*;
use dioxus_web::Config;
2022-01-07 05:33:09 +00:00
use web_sys::window;
fn app() -> Element {
2024-01-14 05:12:21 +00:00
rsx! {
div { h1 { "thing 1" } }
div { h2 { "thing 2" } }
2022-01-07 05:33:09 +00:00
div {
h2 { "thing 2" }
2022-01-07 05:33:09 +00:00
"asd"
"asd"
Bapp {}
2022-01-07 05:33:09 +00:00
}
{(0..10).map(|f| rsx!{
2022-01-07 05:33:09 +00:00
div {
"thing {f}"
}
})}
2024-01-14 05:12:21 +00:00
}
2022-01-07 05:33:09 +00:00
}
#[allow(non_snake_case)]
fn Bapp() -> Element {
2024-01-14 05:12:21 +00:00
rsx! {
div { h1 { "thing 1" } }
div { h2 { "thing 2" } }
2022-01-07 05:33:09 +00:00
div {
h2 { "thing 2" }
2022-01-07 05:33:09 +00:00
"asd"
"asd"
}
2024-01-14 05:12:21 +00:00
}
2022-01-07 05:33:09 +00:00
}
fn main() {
console_error_panic_hook::set_once();
2023-09-12 14:07:57 +00:00
tracing_wasm::set_as_global_default();
2022-01-07 05:33:09 +00:00
let mut dom = VirtualDom::new(app);
2024-02-05 07:03:52 +00:00
dom.rebuild(&mut dioxus_core::NoOpMutations);
2022-01-07 05:33:09 +00:00
let pre = dioxus_ssr::pre_render(&dom);
2023-09-06 22:47:33 +00:00
tracing::trace!("{}", pre);
2022-01-07 05:33:09 +00:00
// set the inner content of main to the pre-rendered content
window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("main")
.unwrap()
.set_inner_html(&pre);
// now rehydrate
dioxus_web::launch::launch(app, vec![], Config::new().hydrate(true));
2022-01-07 05:33:09 +00:00
}