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

yew/functional/hooks/use_prepared_state/
feat_hydration_ssr.rs

1//! The client-and-server-side rendering variant.
2
3use std::future::Future;
4use std::rc::Rc;
5
6use serde::de::DeserializeOwned;
7use serde::Serialize;
8
9use super::{feat_hydration, feat_ssr};
10use crate::functional::{Hook, HookContext};
11use crate::html::RenderMode;
12use crate::suspense::SuspensionResult;
13
14#[doc(hidden)]
15pub fn use_prepared_state<T, D, F>(
16    deps: D,
17    f: F,
18) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
19where
20    D: Serialize + DeserializeOwned + PartialEq + 'static,
21    T: Serialize + DeserializeOwned + 'static,
22    F: FnOnce(Rc<D>) -> T,
23{
24    struct HookProvider<T, D, F>
25    where
26        D: Serialize + DeserializeOwned + PartialEq + 'static,
27        T: Serialize + DeserializeOwned + 'static,
28        F: FnOnce(Rc<D>) -> T,
29    {
30        deps: D,
31        f: F,
32    }
33
34    impl<T, D, F> Hook for HookProvider<T, D, F>
35    where
36        D: Serialize + DeserializeOwned + PartialEq + 'static,
37        T: Serialize + DeserializeOwned + 'static,
38        F: FnOnce(Rc<D>) -> T,
39    {
40        type Output = SuspensionResult<Option<Rc<T>>>;
41
42        fn run(self, ctx: &mut HookContext) -> Self::Output {
43            match ctx.creation_mode {
44                RenderMode::Ssr => feat_ssr::use_prepared_state(self.deps, self.f).run(ctx),
45                _ => feat_hydration::use_prepared_state(self.deps).run(ctx),
46            }
47        }
48    }
49
50    HookProvider::<T, D, F> { deps, f }
51}
52
53#[doc(hidden)]
54pub fn use_prepared_state_with_suspension<T, D, F, U>(
55    deps: D,
56    f: F,
57) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
58where
59    D: Serialize + DeserializeOwned + PartialEq + 'static,
60    T: Serialize + DeserializeOwned + 'static,
61    F: FnOnce(Rc<D>) -> U,
62    U: 'static + Future<Output = T>,
63{
64    struct HookProvider<T, D, F, U>
65    where
66        D: Serialize + DeserializeOwned + PartialEq + 'static,
67        T: Serialize + DeserializeOwned + 'static,
68        F: FnOnce(Rc<D>) -> U,
69        U: 'static + Future<Output = T>,
70    {
71        deps: D,
72        f: F,
73    }
74
75    impl<T, D, F, U> Hook for HookProvider<T, D, F, U>
76    where
77        D: Serialize + DeserializeOwned + PartialEq + 'static,
78        T: Serialize + DeserializeOwned + 'static,
79        F: FnOnce(Rc<D>) -> U,
80        U: 'static + Future<Output = T>,
81    {
82        type Output = SuspensionResult<Option<Rc<T>>>;
83
84        fn run(self, ctx: &mut HookContext) -> Self::Output {
85            match ctx.creation_mode {
86                RenderMode::Ssr => {
87                    feat_ssr::use_prepared_state_with_suspension(self.deps, self.f).run(ctx)
88                }
89                _ => feat_hydration::use_prepared_state(self.deps).run(ctx),
90            }
91        }
92    }
93
94    HookProvider::<T, D, F, U> { deps, f }
95}