This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
mod use_callback;
mod use_context;
mod use_effect;
mod use_force_update;
mod use_memo;
mod use_prepared_state;
mod use_reducer;
mod use_ref;
mod use_state;
mod use_transitive_state;

pub use use_callback::*;
pub use use_context::*;
pub use use_effect::*;
pub use use_force_update::*;
pub use use_memo::*;
pub use use_prepared_state::*;
pub use use_reducer::*;
pub use use_ref::*;
pub use use_state::*;
pub use use_transitive_state::*;

use crate::functional::HookContext;

/// A trait that is implemented on hooks.
///
/// Hooks are defined via the [`#[hook]`](crate::functional::hook) macro. It provides rewrites to
/// hook invocations and ensures that hooks can only be called at the top-level of a function
/// component or a hook. Please refer to its documentation on how to implement hooks.
pub trait Hook {
    /// The return type when a hook is run.
    type Output;

    /// Runs the hook inside current state, returns output upon completion.
    fn run(self, ctx: &mut HookContext) -> Self::Output;
}

/// The blanket implementation of boxed hooks.
#[doc(hidden)]
#[allow(missing_debug_implementations, missing_docs)]
pub struct BoxedHook<'hook, T> {
    inner: Box<dyn 'hook + FnOnce(&mut HookContext) -> T>,
}

impl<'hook, T> BoxedHook<'hook, T> {
    #[allow(missing_docs)]
    pub fn new(inner: Box<dyn 'hook + FnOnce(&mut HookContext) -> T>) -> Self {
        Self { inner }
    }
}

impl<T> Hook for BoxedHook<'_, T> {
    type Output = T;

    fn run(self, ctx: &mut HookContext) -> Self::Output {
        (self.inner)(ctx)
    }
}