Function yewdux::functional::use_selector
source · pub fn use_selector<'hook, S, F, R>(
selector: F
) -> impl 'hook + Hook<Output = Rc<R>>
Expand description
Provides access to some derived portion of state. Useful when you only want to rerender when that portion has changed.
Example
use yew::prelude::*;
use yewdux::prelude::*;
#[derive(Default, Clone, PartialEq, Store)]
struct State {
count: u32,
}
#[function_component]
fn App() -> Html {
let dispatch = use_dispatch::<State>();
let count = use_selector(|state: &State| state.count);
let onclick = dispatch.reduce_mut_callback(|state| state.count += 1);
html! {
<>
<p>{ *count }</p>
<button {onclick}>{"+1"}</button>
</>
}
}
Note
When used in function components and hooks, this hook is equivalent to:
pub fn use_selector<S, F, R>(selector: F) -> Rc<R>
where
S: Store,
R: PartialEq + 'static,
F: Fn(&S) -> R + 'static,
{
/* implementation omitted */
}