allow passing root prop data from the server to the client

This commit is contained in:
Evan Almloff committed 2023-04-28 17:59:01 -05:00
1 parent 0726a7b08f
commit 03a2824660
12 files changed
+230 -21

No files matched your search

@@ -8,10 +8,15 @@
#![allow(non_snake_case)]
use dioxus::prelude::*;
use dioxus_server::prelude::*;
use serde::{Deserialize, Serialize};
fn main() {
#[cfg(feature = "web")]
dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true));
dioxus_web::launch_with_props(
app,
get_props_from_document().unwrap_or_default(),
dioxus_web::Config::new().hydrate(true),
);
#[cfg(feature = "ssr")]
{
PostServerData::register().unwrap();
@@ -23,7 +28,10 @@ fn main() {
axum::Server::bind(&addr)
.serve(
axum::Router::new()
.serve_dioxus_application("", ServeConfigBuilder::new(app, ()))
.serve_dioxus_application(
"",
ServeConfigBuilder::new(app, AppProps { count: 12345 }).build(),
)
.into_make_service(),
)
.await
@@ -32,8 +40,13 @@ fn main() {
}
}
fn app(cx: Scope) -> Element {
let mut count = use_state(cx, || 0);
#[derive(Props, PartialEq, Debug, Default, Serialize, Deserialize, Clone)]
struct AppProps {
count: i32,
}
fn app(cx: Scope<AppProps>) -> Element {
let mut count = use_state(cx, || cx.props.count);
let text = use_state(cx, || "...".to_string());
cx.render(rsx! {