Function use_slice_dispatch

Source
pub fn use_slice_dispatch<'hook, T>() -> impl 'hook + Hook<Output = Rc<dyn Fn(T::Action)>>
where T: Slice + 'static + 'hook,
Expand description

A hook to produce a dispatch function for a Slice.

Returns a Rc<dyn Fn(T::Action)>.

This hook will return a dispatch function that will not change across the entire lifetime of the component.

§Example

#[function_component(CounterComp)]
fn counter_comp() -> Html {
    let dispatch_ctr = use_slice_dispatch::<Counter>();

    let inc = {
        let dispatch_ctr = dispatch_ctr.clone();
        Callback::from(move |_| {dispatch_ctr(CounterAction::Increment);})
    };
    let dec = {
        let dispatch_ctr = dispatch_ctr.clone();
        Callback::from(move |_| {dispatch_ctr(CounterAction::Decrement);})
    };

    html! {
        <div>
            <button onclick={inc}>{"Increase"}</button>
            <button onclick={dec}>{"Decrease"}</button>
        </div>
    }
}

§Note

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

pub fn use_slice_dispatch<T>() -> Rc<dyn Fn(T::Action)>
where
    T: Slice + 'static,
{
    /* implementation omitted */
}