Function use_interval

Source
pub fn use_interval<'hook, Callback>(
    callback: Callback,
    millis: u32,
) -> impl 'hook + Hook<Output = ()>
where Callback: FnMut() + 'static + 'hook,
Expand description

A hook that schedules an interval to invoke callback every millis milliseconds. The interval will be cancelled if millis is set to 0.

§Example

use yew_hooks::prelude::*;

#[function_component(Interval)]
fn interval() -> Html {
    let state = use_state(|| 0);

    {
        let state = state.clone();
        use_interval(move || {
            state.set(*state + 1);
        }, 2000);
    }
    
    html! {
        <>
            { *state }
        </>
    }
}

§Note

When used in function components and hooks, this hook is equivalent to:

pub fn use_interval<Callback>(callback: Callback, millis: u32)
where
    Callback: FnMut() + 'static,
{
    /* implementation omitted */
}