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_throttled

A throttled version of leptos::watch.

Demo

Usage

use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_watch_debounce_throttle::watch_throttled;

pub fn Demo() -> impl IntoView {
    let (source, set_source) = signal(0);

watch_throttled(
    move || source.get(),
    move |_, _, _| {
        log!("changed!");
    },
    500.0,
);

   view! { }
}

This really is only shorthand shorthand for watch_with_options(deps, callback, WatchOptions::default().throttle(ms)).

Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.

There’s also watch_throttled_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_throttled_with_options, WatchThrottledOptions};

pub fn Demo() -> impl IntoView {
    let (source, set_source) = signal(0);

watch_throttled_with_options(
    move || source.get(),
    move |_, _, _| {
        log!("changed!");
    },
    500.0,
    WatchThrottledOptions::default().leading(true).trailing(false),
);

   view! { }
}

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

Installation

cargo add leptos-use-watch-debounce-throttle

Options

WatchThrottledOptions

Options for watch_throttled_with_options.

Every option is set with a builder method of the same name, e.g. WatchThrottledOptions::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.

trailing

Type: bool

Invoke on the trailing edge of the timeout. Defaults to true.

leading

Type: bool

Invoke on the leading edge of the timeout. Defaults to true.

Source

Source • Demo • Docs