whenever
Shorthand for watching a signal to be true.
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_whenever::whenever;
pub fn Demo() -> impl IntoView {
let (is_ready, set_ready) = signal(false);
whenever(move || is_ready.get(), |v, _, _| log!("{}", v));
view! { }
}
Callback Function
Same as watch, the callback will be called with callback(input, prev_input, prev_return).
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_whenever::whenever;
pub fn Demo() -> impl IntoView {
let (is_ready, set_ready) = signal(false);
whenever(move || is_ready.get(), |value, prev_value, _| {
log!("before: {prev_value:?}; now: {value}");
});
view! { }
}
Computed
Same as watch, you can pass a getter function to calculate on each change.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_whenever::whenever;
pub fn Demo() -> impl IntoView {
let (counter, set_counter) = signal(0);
whenever(
move || counter.get() == 7,
|_, _, _| log!("counter is 7 now!"),
);
view! { }
}
Options
Options and defaults are same as watch_with_options.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_watch_with_options::WatchOptions;
use leptos_use_whenever::whenever_with_options;
pub fn Demo() -> impl IntoView {
let (counter, set_counter) = signal(0);
whenever_with_options(
move || counter.get() == 7,
|_, _, _| log!("counter is 7 now!"),
WatchOptions::default().immediate(true),
);
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.
Installation
cargo add leptos-use-whenever
Options
WatchOptions
Options for watch_with_options
The filter is a type parameter so that the debounce and throttle code is only compiled into your app if you use it.
Every option is set with a builder method of the same name, e.g. WatchOptions::default().immediate(...). See also the API docs.
immediate
Type: bool
If immediate is true, the callback will run immediately.
If it’s false, the callbackwill run only after the first change is detected of any signal that is accessed indeps. Defaults to false`.
filter
Type: F
Allows to debounce or throttle the callback with a FilterOptions. Defaults to no filter.