document from_server

This commit is contained in:
Evan Almloff 2023-07-14 17:39:46 -07:00
parent de72d85391
commit 040b9d15a8
2 changed files with 19 additions and 3 deletions

View file

@ -60,5 +60,5 @@ pub mod prelude {
pub use dioxus_ssr::incremental::IncrementalRendererConfig;
pub use server_fn::{self, ServerFn as _, ServerFnError};
pub use use_server::server_cached;
pub use use_server::from_server;
}

View file

@ -1,7 +1,23 @@
use serde::{de::DeserializeOwned, Serialize};
/// TODO: Document this
pub fn server_cached<O: 'static + Serialize + DeserializeOwned>(server_fn: impl Fn() -> O) -> O {
/// This allows you to send data from the server to the client. The data is serialized into the HTML on the server and hydrated on the client.
///
/// When you run this function on the client, you need to be careful to insure the order you run it initially is the same order you run it on the server.
///
/// If Dioxus fullstack cannot find the data on the client, it will run the closure again to get the data.
///
/// # Example
/// ```rust
/// use dioxus::prelude::*;
/// use dioxus_fullstack::prelude::*;
///
/// fn app(cx: Scope) -> Element {
/// let state1 = use_state(cx, || from_server(|| {
/// 1234
/// }));
/// }
/// ```
pub fn from_server<O: 'static + Serialize + DeserializeOwned>(server_fn: impl Fn() -> O) -> O {
#[cfg(feature = "ssr")]
{
let data =