Function leptos_use::use_raf_fn

source ·
pub fn use_raf_fn(
    callback: impl Fn(UseRafFnCallbackArgs) + 'static
) -> Pausable<impl Fn() + Clone, impl Fn() + Clone>
Expand description

Call function on every requestAnimationFrame. With controls of pausing and resuming.

§Demo

Link to Demo

§Usage

use leptos_use::utils::Pausable;
let (count, set_count) = create_signal(0);

let Pausable { pause, resume, is_active } = use_raf_fn(move |_| {
    set_count.update(|count| *count += 1);
});

view! { <div>Count: { count }</div> }
}

You can use use_raf_fn_with_options and set immediate to false. In that case you have to call resume() before the callback is executed.

§Server-Side Rendering

On the server this does basically nothing. The provided closure will never be called.