Leptos-Use Guide

signal_throttled

Throttle changing of a Signal value.

Use *_local variants for values that are not Send + Sync.

Demo

Usage

use leptos::prelude::*;
use leptos_use::{signal_throttled, signal_throttled_local};
use std::cell::RefCell;

#[component]
fn Demo() -> impl IntoView {
let (input, set_input) = signal("");
let throttled: Signal<&'static str> = signal_throttled(input, 1000.0);

let (input_local, set_input_local) = signal_local(RefCell::new(0));
let throttled_local: Signal<RefCell<i32>, _> = signal_throttled_local(input_local, 1000.0);

view! { }
}

Options

The usual throttle options leading and trailing are available.

use leptos::prelude::*;
use leptos_use::{signal_throttled_with_options, signal_throttled_local_with_options, ThrottleOptions};
use std::cell::RefCell;

#[component]
fn Demo() -> impl IntoView {
let (input, set_input) = signal("");
let throttled: Signal<&'static str> = signal_throttled_with_options(
    input,
    1000.0,
    ThrottleOptions::default().leading(false).trailing(true)
);

let (input_local, set_input_local) = signal_local(RefCell::new(0));
let throttled_local: Signal<RefCell<i32>, _> = signal_throttled_local_with_options(
    input_local,
    1000.0,
    ThrottleOptions::default().leading(false).trailing(true)
);

view! { }
}

Server-Side Rendering

Internally this uses setTimeout which is not supported on the server. So usually a throttled signal on the server will simply be ignored.

Feature

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

Source

SourceDemoDocs