watch_throttled
A throttled version of leptos::watch
.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::watch_throttled;
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_throttled(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
);
view! { }
}
This really is only shorthand shorthand for watch_with_options(deps, callback, WatchOptions::default().throttle(ms))
.
Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
There's also watch_throttled_with_options
where you can specify the other watch options (except filter
).
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_throttled_with_options, WatchThrottledOptions};
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_throttled_with_options(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
WatchThrottledOptions::default().leading(true).trailing(false),
);
view! { }
}
Recommended Reading
Server-Side Rendering
On the server the callback
will never be called except if you set immediate
to true
in which case the callback will be
called exactly once.
See also
leptos::watch
watch_debounced
Feature
This function is only available if the crate feature
watch_throttled
is enabled