This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on

Macro yew::functional::use_transitive_state

source ·
use_transitive_state!() { /* proc-macro */ }
Expand description

Use a state created as an artifact of the server-side rendering.

This value is created after the server-side rendering artifact is created.

It accepts a closure as the first argument and a dependency type as the second argument. It returns SuspensionResult<Option<Rc<T>>>.

It will always return Ok(None) during server-side rendering.

During hydration, it will only return Ok(Some(Rc<T>)) if the component is hydrated from a server-side rendering artifact and its dependency value matches.

let state = use_transitive_state!(deps, |deps| -> ReturnType { ... });

It has the following function signature:

use yew::prelude::*;
use yew::suspense::SuspensionResult;

pub fn use_transitive_state<T, D, F>(deps: D, f: F) -> SuspensionResult<Option<Rc<T>>>
    D: Serialize + DeserializeOwned + PartialEq + 'static,
    T: Serialize + DeserializeOwned + 'static,
    F: 'static + FnOnce(Rc<D>) -> T,

If the bundle is compiled without server-side rendering, the closure will be stripped automatically.


You MUST denote the return type of the closure with |deps| -> ReturnType { ... }. This type is used during client side rendering to deserialize the state prepared on the server side.