watch_with_options
A version of leptos::watch but with additional options.
Immediate
This is the same as for leptos::watch. But you don’t have to specify it.
By default its set to false.
If immediate is true, the callback will run immediately (this is also true if throttled/debounced).
If it’s false, the callback will run only after
the first change is detected of any signal that is accessed in deps.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_with_options, WatchOptions};
pub fn Demo() -> impl IntoView {
let (num, set_num) = signal(0);
watch_with_options(
move || num.get(),
move |num, _, _| {
log!("Number {}", num);
},
WatchOptions::default().immediate(true),
); // > "Number 0"
set_num.set(1); // > "Number 1"
view! { }
}
Filters
The callback can be throttled or debounced. Please see fn@crate::watch_throttled
and fn@crate::watch_debounced for details.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_with_options, WatchOptions};
pub fn Demo() -> impl IntoView {
let (num, set_num) = signal(0);
watch_with_options(
move || num.get(),
move |num, _, _| {
log!("Number {}", num);
},
WatchOptions::default().throttle(100.0), // there's also `throttle_with_options`
);
view! { }
}
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_with_options, WatchOptions};
pub fn Demo() -> impl IntoView {
let (num, set_num) = signal(0);
watch_with_options(
move || num.get(),
move |num, _, _| {
log!("number {}", num);
},
WatchOptions::default().debounce(100.0), // there's also `debounce_with_options`
);
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this works just fine except if you throttle or debounce in which case the callback
will never be called except if you set immediate to true in which case the callback will be
called exactly once when watch() is executed.
See also
Feature
This function is only available if the crate feature
watch_with_optionsis enabled