yew_hooks/hooks/use_mount.rs
1use yew::prelude::*;
2
3use super::use_effect_once;
4
5/// A lifecycle hook that calls a function after the component is mounted.
6///
7/// # Example
8///
9/// ```rust
10/// # use yew::prelude::*;
11/// # use log::debug;
12/// #
13/// use yew_hooks::prelude::*;
14///
15/// #[function_component(Mount)]
16/// fn mount() -> Html {
17/// use_mount(|| {
18/// debug!("Running effect once on mount");
19/// });
20///
21/// html! {
22/// <>
23/// </>
24/// }
25/// }
26/// ```
27#[hook]
28pub fn use_mount<Callback>(callback: Callback)
29where
30 Callback: FnOnce() + 'static,
31{
32 use_effect_once(move || {
33 callback();
34
35 || ()
36 });
37}