Function use_previous

Source
pub fn use_previous<'hook, T>(
    value: T,
) -> impl 'hook + Hook<Output = UsePreviousHandle<T>>
where T: 'static + 'hook,
Expand description

This hook returns the previous immutable ref to state or props.

§Example

use yew_hooks::prelude::*;

#[function_component(UsePrevious)]
fn previous() -> Html {
    let state = use_state(|| 0);
    let previous_state = use_previous(state.clone());


    let onincrease = {
        let state = state.clone();
        Callback::from(move |_| state.set(*state + 1))
    };
    let ondecrease = {
        let state = state.clone();
        Callback::from(move |_| state.set(*state - 1))
    };
    
    html! {
        <div>
            <button onclick={onincrease}>{ "Increase" }</button>
            <button onclick={ondecrease}>{ "Decrease" }</button>
            <p>
                <b>{ "Current value: " }</b>
                { *state }
            </p>
            <p>
                <b>{ "Previous value: " }</b>
                { **previous_state }
            </p>
        </div>
    }
}

§Note

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

pub fn use_previous<T>(value: T) -> UsePreviousHandle<T>
where
    T: 'static,
{
    /* implementation omitted */
}