Function use_queue

Source
pub fn use_queue<'hook, T>(
    initial_value: VecDeque<T>,
) -> impl 'hook + Hook<Output = UseQueueHandle<T>>
where T: 'static + 'hook,
Expand description

A hook that tracks a queue and provides methods to modify it.

§Example

use yew_hooks::prelude::*;

#[function_component(UseQueue)]
fn queue() -> Html {
    let queue = use_queue(VecDeque::from(["Mercury", "Venus", "Earth", "Mars"]));

    let onset = {
        let queue = queue.clone();
        Callback::from(move |_| queue.set(VecDeque::from(["Moon", "Earth"])))
    };
    let onpush_back = {
        let queue = queue.clone();
        Callback::from(move |_| {
            queue.push_back("Jupiter");
        })
    };
    let onpop_front = {
        let queue = queue.clone();
        Callback::from(move |_| {
            let _ = queue.pop_front();
        })
    };
    let onretain = {
        let queue = queue.clone();
        Callback::from(move |_| queue.retain(|v| v.contains('a')))
    };
    let onclear = {
        let queue = queue.clone();
        Callback::from(move |_| queue.clear())
    };

    html! {
        <div>
            <button onclick={onset}>{ "Set" }</button>
            <button onclick={onpush_back}>{ "Push back" }</button>
            <button onclick={onpop_front}>{ "Pop front" }</button>
            <button onclick={onretain}>{ "Retain" }</button>
            <button onclick={onclear}>{ "Clear all" }</button>
            <p>
                <b>{ "Current value: " }</b>
            </p>
            {
                for queue.current().iter().map(|v| {
                    html! {
                        <p><b>{ v }</b></p>
                    }
                })
            }
        </div>
    }
}

§Note

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

pub fn use_queue<T>(initial_value: VecDeque<T>) -> UseQueueHandle<T>
where
    T: 'static,
{
    /* implementation omitted */
}