yew_hooks/hooks/
use_is_first_mount.rs

1use yew::prelude::*;
2
3/// A hook returns true if component is just mounted (on first render) and false otherwise.
4///
5/// # Example
6///
7/// ```rust
8/// # use yew::prelude::*;
9/// #
10/// use yew_hooks::prelude::*;
11///
12/// #[function_component(IsFirstMount)]
13/// fn is_first_mount() -> Html {
14///     let is_first = use_is_first_mount();
15///     
16///     html! {
17///         <>
18///             { is_first }
19///         </>
20///     }
21/// }
22/// ```
23#[hook]
24pub fn use_is_first_mount() -> bool {
25    let is_first = use_mut_ref(|| true);
26
27    if *is_first.borrow_mut() {
28        *is_first.borrow_mut() = false;
29
30        return true;
31    }
32
33    false
34}