dioxus/packages/ssr/examples/tide.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

2021-05-27 21:57:59 +00:00
//!
//!
//!
2021-07-11 21:24:47 +00:00
use dioxus::virtual_dom::VirtualDom;
use dioxus_core as dioxus;
2021-05-27 21:57:59 +00:00
use dioxus_core::prelude::*;
2021-09-25 01:46:23 +00:00
use dioxus_core_macro::*;
2021-07-11 21:24:47 +00:00
use dioxus_hooks::use_state;
use dioxus_html as dioxus_elements;
use tide::{Request, Response};
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/:name").get(|req: Request<()>| async move {
let initial_name: String = req
.param("name")
.map(|f| f.parse().unwrap_or("...?".to_string()))
.unwrap_or("...?".to_string());
2021-08-24 19:12:20 +00:00
let mut dom = VirtualDom::new_with_props(Example, ExampleProps { initial_name });
dom.rebuild();
2021-07-11 21:24:47 +00:00
Ok(Response::builder(200)
.body(format!("{}", dioxus_ssr::render_vdom(&dom, |c| c)))
2021-07-11 21:24:47 +00:00
.content_type(tide::http::mime::HTML)
.build())
});
println!("Server available at [http://127.0.0.1:8080/bill]");
app.listen("127.0.0.1:8080").await?;
Ok(())
}
2021-05-27 21:57:59 +00:00
#[derive(PartialEq, Props)]
struct ExampleProps {
initial_name: String,
}
2021-09-21 17:42:52 +00:00
static Example: FC<ExampleProps> = |cx, props| {
let dispaly_name = use_state(cx, move || props.initial_name.clone());
2021-05-27 21:57:59 +00:00
2021-06-26 01:15:33 +00:00
cx.render(rsx! {
2021-07-11 21:24:47 +00:00
div { class: "py-12 px-4 text-center w-full max-w-2xl mx-auto",
span { class: "text-sm font-semibold"
2021-05-27 21:57:59 +00:00
"Dioxus Example: Jack and Jill"
}
2021-07-11 21:24:47 +00:00
h2 { class: "text-5xl mt-2 mb-6 leading-tight font-semibold font-heading"
2021-05-27 21:57:59 +00:00
"Hello, {dispaly_name}"
}
2021-07-11 21:24:47 +00:00
ul {
{(0..10).map(|f| rsx!( li {"Element {f}"} ))}
}
2021-05-27 21:57:59 +00:00
}
})
};