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

yew/functional/hooks/use_transitive_state/
feat_hydration_ssr.rs

1//! The client-and-server-side rendering variant.
2
3use std::rc::Rc;
4
5use serde::de::DeserializeOwned;
6use serde::Serialize;
7
8use super::{feat_hydration, feat_ssr};
9use crate::functional::{Hook, HookContext};
10use crate::html::RenderMode;
11use crate::suspense::SuspensionResult;
12
13#[doc(hidden)]
14pub fn use_transitive_state<T, D, F>(
15    deps: D,
16    f: F,
17) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
18where
19    D: Serialize + DeserializeOwned + PartialEq + 'static,
20    T: Serialize + DeserializeOwned + 'static,
21    F: 'static + FnOnce(Rc<D>) -> T,
22{
23    struct HookProvider<T, D, F>
24    where
25        D: Serialize + DeserializeOwned + PartialEq + 'static,
26        T: Serialize + DeserializeOwned + 'static,
27        F: 'static + FnOnce(Rc<D>) -> T,
28    {
29        deps: D,
30        f: F,
31    }
32
33    impl<T, D, F> Hook for HookProvider<T, D, F>
34    where
35        D: Serialize + DeserializeOwned + PartialEq + 'static,
36        T: Serialize + DeserializeOwned + 'static,
37        F: 'static + FnOnce(Rc<D>) -> T,
38    {
39        type Output = SuspensionResult<Option<Rc<T>>>;
40
41        fn run(self, ctx: &mut HookContext) -> Self::Output {
42            match ctx.creation_mode {
43                RenderMode::Ssr => feat_ssr::use_transitive_state(self.deps, self.f).run(ctx),
44                _ => feat_hydration::use_transitive_state(self.deps).run(ctx),
45            }
46        }
47    }
48
49    HookProvider::<T, D, F> { deps, f }
50}