Function use_state_ptr_eq

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

Similar to use_state_eq, but check if the two Rcs of values point to the same allocation, instead of PartialEq of the values.

§Example

use yew_hooks::prelude::*;

#[function_component(UseStatePtrEq)]
fn state_ptr_eq() -> Html {
    let state = use_state_ptr_eq(|| "".to_string());

    let onclick = {
        let state = state.clone();
        Callback::from(move |_| {
            state.set("Hello, world!".to_string());
        })
    };
    
    html! {
        <>
            <button {onclick}>{ "Hello, world!" }</button>
            <p>
                <b>{ "Current value: " }</b>
                { &*state }
            </p>
        </>
    }
}

§Note

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

pub fn use_state_ptr_eq<T, F>(init_fn: F) -> UseStatePtrEqHandle<T>
where
    T: 'static,
    F: FnOnce() -> T,
{
    /* implementation omitted */
}