Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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::{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 watch_throttled and watch_debounced for details.

use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_watch_with_options::{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::{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

Installation

cargo add leptos-use-watch-with-options

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.

Source

Source • Docs