watch_debounced
A debounced version of watch.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_watch_debounce_throttle::watch_debounced;
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_debounced(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
);
view! { }
}
This really is only shorthand shorthand for watch_with_options(deps, callback, WatchOptions::default().debounce(ms)).
Please note that if the current component is cleaned up before the debounced callback is called, the debounced callback will not be called.
There’s also watch_debounced_with_options where you can specify the other watch options (except filter).
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_watch_debounce_throttle::{watch_debounced_with_options, WatchDebouncedOptions};
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_debounced_with_options(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
WatchDebouncedOptions::default().max_wait(Some(1000.0)),
);
view! { }
}
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in 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::watchwatch_throttled
Installation
cargo add leptos-use-watch-debounce-throttle
Options
WatchDebouncedOptions
Options for watch_debounced_with_options.
Every option is set with a builder method of the same name, e.g. WatchDebouncedOptions::default().immediate(...). See also the API docs.
immediate
Type: bool
If immediate is false, the callback will not run immediately but only after
the first change is detected of any signal that is accessed in deps.
Defaults to false.
max_wait
Type: Signal<Option<f64>>
The maximum time allowed to be delayed before the callback invoked.
In milliseconds.
Same as DebounceOptions::max_wait