Function freya::prelude::use_effect

pub fn use_effect<T, F, D>(
    cx: &ScopeState,
    dependencies: D,
    future: impl FnOnce(<D as UseFutureDep>::Out) -> F
)where
    T: 'static,
    F: Future<Output = T> + 'static,
    D: UseFutureDep,
Expand description

A hook that provides a future that executes after the hooks have been applied

Whenever the hooks dependencies change, the future will be re-evaluated. If a future is pending when the dependencies change, the previous future will be allowed to continue

  • dependencies: a tuple of references to values that are PartialEq + Clone

Examples

#[inline_props]
fn Profile(cx: Scope, id: usize) -> Element {
    let name = use_state(cx, || None);

    // Only fetch the user data when the id changes.
    use_effect(cx, (id,), |(id,)| {
        to_owned![name];
        async move {
            let user = fetch_user(id).await;
            name.set(user.name);
        }
    });

    let name = name.get().clone().unwrap_or("Loading...".to_string());

    render!(
        p { "{name}" }
    )
}

fn app(cx: Scope) -> Element {
    render!(Profile { id: 0 })
}