Leptos-Use Guide

watch_throttled

A throttled version of leptos::watch.

Demo

Usage

use leptos::*;
use leptos::logging::log;
use leptos_use::watch_throttled;

pub fn Demo() -> impl IntoView {
    let (source, set_source) = create_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::*;
use leptos::logging::log;
use leptos_use::{watch_throttled_with_options, WatchThrottledOptions};

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

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

   view! { }
}

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

Feature

This function is only available if the crate feature watch_throttled is enabled

Types

Source

SourceDemoDocs