Function use_debounce_state

Source
pub fn use_debounce_state<'hook, T, F>(
    init_fn: F,
    millis: u32,
) -> impl 'hook + Hook<Output = UseDebounceStateHandle<T>>
where T: 'static + 'hook, F: FnOnce() -> T + 'hook,
Expand description

A hook that delays updating state until after wait milliseconds have elapsed since the last time state was updated.

§Example

use yew_hooks::prelude::*;

#[function_component(DebounceState)]
fn debounce_state() -> Html {
    let state = use_debounce_state(|| "".to_string(), 2000);

    let oninput = {
        let state = state.clone();
        Callback::from(move |e: InputEvent| {
            let input: HtmlInputElement = e.target_unchecked_into();
            state.set(input.value());
        })
    };

    html! {
        <>
            <input type="text" placeholder="Debounced input" {oninput}/>
            <b>{ "Debounced state: " }</b> {&*state}
        </>
    }
}

§Note

When used in function components and hooks, this hook is equivalent to:

pub fn use_debounce_state<T, F>(init_fn: F, millis: u32) -> UseDebounceStateHandle<T>
where
    T: 'static,
    F: FnOnce() -> T,
{
    /* implementation omitted */
}