dioxus/examples/ssr.rs

40 lines
914 B
Rust
Raw Normal View History

//! Example: SSR
//!
//! This example shows how we can render the Dioxus Virtualdom using SSR.
use dioxus::prelude::*;
2021-07-11 23:31:07 +00:00
fn main() {
// We can render VirtualDoms
let mut vdom = VirtualDom::prebuilt(app);
println!("{}", dioxus_ssr::render(&vdom));
2024-01-16 19:18:46 +00:00
// Or we can render rsx! calls themselves
println!(
"{}",
2024-01-16 19:18:46 +00:00
dioxus_ssr::render_element(rsx! {
div {
h1 { "Hello, world!" }
}
})
);
// We can configure the SSR rendering to add ids for rehydration
println!("{}", dioxus_ssr::pre_render(&vdom));
// We can render to a buf directly too
let mut file = String::new();
let mut renderer = dioxus_ssr::Renderer::default();
renderer.render_to(&mut file, &vdom).unwrap();
2023-01-28 02:35:46 +00:00
println!("{file}");
2021-07-11 23:31:07 +00:00
}
fn app() -> Element {
2024-01-16 19:18:46 +00:00
rsx!(
2021-07-11 23:31:07 +00:00
div {
h1 { "Title" }
p { "Body" }
}
2024-01-15 19:54:17 +00:00
)
}