Function use_selector_value

Source
pub fn use_selector_value<'hook, T>() -> impl 'hook + Hook<Output = Rc<T>>
where T: Selector + 'static + 'hook,
Expand description

A hook to connect to a Selector.

A selector is a derived state which its value is derived from other states.

Its value will be automatically re-calculated when any state used in the selector has changed.

Returns a Rc<T>.

§Example

#[derive(Default, PartialEq, Slice)]
struct Value(i64);

#[derive(PartialEq)]
pub struct IsEven {
    inner: bool,
}

impl Selector for IsEven {
    fn select(states: &BounceStates) -> Rc<Self> {
        let val = states.get_slice_value::<Value>();

        Self {
            inner: val.0 % 2 == 0,
        }
        .into()
    }
}
let is_even = use_selector_value::<IsEven>();

§Note

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

pub fn use_selector_value<T>() -> Rc<T>
where
    T: Selector + 'static,
{
    /* implementation omitted */
}