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
use dioxus_core::ScopeState;
use std::cell::Cell;
use std::future::Future;

/// A hook that runs a future when the component is mounted.
///
/// This is just [`use_effect`](crate::use_effect), but with no dependencies.
/// If you have no dependencies, it's recommended to use this, not just because it's more readable,
/// but also because it's a tiny bit more efficient.
pub fn use_on_create<T, F>(cx: &ScopeState, future: impl FnOnce() -> F)
where
    T: 'static,
    F: Future<Output = T> + 'static,
{
    let needs_regen = cx.use_hook(|| Cell::new(true));

    if needs_regen.get() {
        // We don't need regen anymore
        needs_regen.set(false);

        let fut = future();

        cx.push_future(async move {
            fut.await;
        });
    }
}