dioxus/packages/web/examples/hydrate.rs

66 lines
1.3 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(cx: Scope) -> Element {
cx.render(rsx! {
div {
h1 { "thing 1" }
}
div {
h2 { "thing 2"}
}
div {
h2 { "thing 2"}
"asd"
"asd"
Bapp {}
2022-01-07 05:33:09 +00:00
}
(0..10).map(|f| rsx!{
div {
"thing {f}"
}
})
})
}
#[allow(non_snake_case)]
fn Bapp(cx: Scope) -> Element {
2022-01-07 05:33:09 +00:00
cx.render(rsx! {
div {
h1 { "thing 1" }
}
div {
h2 { "thing 2"}
}
div {
h2 { "thing 2"}
"asd"
"asd"
}
})
}
fn main() {
console_error_panic_hook::set_once();
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
let mut dom = VirtualDom::new(app);
let _ = dom.rebuild();
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 rehydtrate
dioxus_web::launch_with_props(app, (), Config::new().hydrate(true));
2022-01-07 05:33:09 +00:00
}