use_server_cached

Function use_server_cached 

Source
pub fn use_server_cached<O, M>(server_fn: impl Fn() -> O) -> O
where O: Transportable<M> + Clone, M: 'static,
Expand description

This allows you to send data from the server to the client during hydration.

  • When compiled as server, the closure is ran and the resulting data is serialized on the server and sent to the client.
  • When compiled as web client, the data is deserialized from the server if already available, otherwise runs on the client. Data is usually only available if this hook exists in a component during hydration.
  • When otherwise compiled, the closure is run directly with no serialization.

The order this function is run on the client needs to be the same order initially run on the server.

If Dioxus fullstack cannot find the data on the client, it will run the closure again to get the data.

ยงExample

use dioxus::prelude::*;

fn app() -> Element {
   let state1 = use_server_cached(|| {
      1234
   });

   unimplemented!()
}