Function yewdux::functional::use_selector_with_deps
source · pub fn use_selector_with_deps<'hook, S, F, R, D>(
selector: F,
deps: D
) -> impl 'hook + Hook<Output = Rc<R>>
Expand description
Similar to use_selector
, but also allows for dependencies from environment. This is
necessary when the derived value uses some captured value.
Example
use std::collections::HashMap;
use yew::prelude::*;
use yewdux::prelude::*;
#[derive(Default, Clone, PartialEq, Store)]
struct State {
user_names: HashMap<u32, String>,
}
#[derive(Properties, PartialEq, Clone)]
struct AppProps {
user_id: u32,
}
#[function_component]
fn ViewName(&AppProps { user_id }: &AppProps) -> Html {
let user_name = use_selector_with_deps(
|state: &State, id| state.user_names.get(id).cloned().unwrap_or_default(),
user_id,
);
html! {
<p>
{ user_name }
</p>
}
}
Note
When used in function components and hooks, this hook is equivalent to:
pub fn use_selector_with_deps<S, F, R, D>(selector: F, deps: D) -> Rc<R>
where
S: Store,
R: PartialEq + 'static,
D: Clone + PartialEq + 'static,
F: Fn(&S, &D) -> R + 'static,
{
/* implementation omitted */
}