Function yew::functional::use_effect[][src]

pub fn use_effect<Destructor>(callback: impl FnOnce() -> Destructor + 'static) where
    Destructor: FnOnce() + 'static, 
Expand description

This hook is used for hooking into the component’s lifecycle.

Example

#[function_component(UseEffect)]
fn effect() -> Html {
    let counter = use_state(|| 0);

    let counter_one = counter.clone();
    use_effect(move || {
        // Make a call to DOM API after component is rendered
        gloo_utils::document().set_title(&format!("You clicked {} times", *counter_one));

        // Perform the cleanup
        || gloo_utils::document().set_title(&format!("You clicked 0 times"))
    });

    let onclick = {
        let counter = counter.clone();
        Callback::from(move |_| counter.set(*counter + 1))
    };

    html! {
        <button {onclick}>{ format!("Increment to {}", *counter) }</button>
    }
}