pub fn use_effect<E: Fn() -> Option<C> + 'static, C: Fn() + 'static, D: Any + PartialEq>(
    effect: E,
    dependencies: Option<D>
) -> UseEffectBuilder<E, D>
Expand description

The Effect Hook lets you perform side effects in components

Examples

Without cleanup function and without dependencies

#[component(Example)]
pub fn example() -> VNode {
    use_effect(
        || {
            log::info!("hello!");
            None::<fn()>
        },
        None::<()>
    );

    Div.into()
}

With cleanup function and without dependencies

#[component(Example)]
pub fn example() -> VNode {
    use_effect(
        || {
            log::info!("hello!");
            Some(|| log::info!("clean"))
        },
        None::<()>
    );

    Div.into()
}

With cleanup function and dependencies

#[component(Example)]
pub fn example() -> VNode {
    use_effect(
        || {
            log::info!("hello!");
            Some(|| log::info!("clean"))
        },
        Some(()) // run only one time because () never change
    );

    Div.into()
}