Function leptos_use::whenever

source ·
pub fn whenever<T, DFn, CFn>(source: DFn, callback: CFn) -> impl Fn() + Clone
where DFn: Fn() -> bool + 'static, CFn: Fn(bool, Option<bool>, Option<T>) -> T + Clone + 'static, T: Clone + 'static,
Expand description

Shorthand for watching a signal to be true.

§Usage

let (is_ready, set_ready) = create_signal(false);

whenever(move || is_ready.get(), |v, _, _| log!("{}", v));

§Callback Function

Same as crate::watch, the callback will be called with callback(input, prev_input, prev_return).

whenever(move || is_ready.get(), |value, prev_value, _| {
    log!("before: {prev_value:?}; now: {value}");
});

§Computed

Same as crate::watch, you can pass a getter function to calculate on each change.

whenever(
    move || counter.get() == 7,
    |_, _, _| log!("counter is 7 now!"),
);

§Options

Options and defaults are same as watch_with_options.

whenever_with_options(
    move || counter.get() == 7,
    |_, _, _| log!("counter is 7 now!"),
    WatchOptions::default().immediate(true),
);

§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.