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

yew/functional/hooks/
mod.rs

1mod use_callback;
2mod use_context;
3mod use_effect;
4mod use_force_update;
5mod use_memo;
6mod use_prepared_state;
7mod use_reducer;
8mod use_ref;
9mod use_state;
10mod use_transitive_state;
11
12pub use use_callback::*;
13pub use use_context::*;
14pub use use_effect::*;
15pub use use_force_update::*;
16pub use use_memo::*;
17pub use use_prepared_state::*;
18pub use use_reducer::*;
19pub use use_ref::*;
20pub use use_state::*;
21pub use use_transitive_state::*;
22
23use crate::functional::HookContext;
24
25/// A trait that is implemented on hooks.
26///
27/// Hooks are defined via the [`#[hook]`](crate::functional::hook) macro. It provides rewrites to
28/// hook invocations and ensures that hooks can only be called at the top-level of a function
29/// component or a hook. Please refer to its documentation on how to implement hooks.
30pub trait Hook {
31    /// The return type when a hook is run.
32    type Output;
33
34    /// Runs the hook inside current state, returns output upon completion.
35    fn run(self, ctx: &mut HookContext) -> Self::Output;
36}
37
38/// The blanket implementation of boxed hooks.
39#[doc(hidden)]
40#[allow(missing_debug_implementations, missing_docs)]
41pub struct BoxedHook<'hook, T> {
42    inner: Box<dyn 'hook + FnOnce(&mut HookContext) -> T>,
43}
44
45impl<'hook, T> BoxedHook<'hook, T> {
46    #[allow(missing_docs)]
47    pub fn new(inner: Box<dyn 'hook + FnOnce(&mut HookContext) -> T>) -> Self {
48        Self { inner }
49    }
50}
51
52impl<T> Hook for BoxedHook<'_, T> {
53    type Output = T;
54
55    fn run(self, ctx: &mut HookContext) -> Self::Output {
56        (self.inner)(ctx)
57    }
58}