Function yewdux::functional::use_store

source ·
pub fn use_store<'hook, S>() -> impl 'hook + Hook<Output = (Rc<S>, Dispatch<S>)>
where S: 'hook + Store,
Expand description

This hook allows accessing the state of a store. When the store is modified, a re-render is automatically triggered.

Example

use yew::prelude::*;
use yewdux::prelude::*;

#[derive(Default, Clone, PartialEq, Store)]
struct State {
    count: u32,
}

#[function_component]
fn App() -> Html {
    let (state, dispatch) = use_store::<State>();
    let onclick = dispatch.reduce_mut_callback(|s| s.count += 1);
    html! {
        <>
        <p>{ state.count }</p>
        <button {onclick}>{"+1"}</button>
        </>
    }
}

Note

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

pub fn use_store<S: Store>() -> (Rc<S>, Dispatch<S>) {
    /* implementation omitted */
}