pub struct Dispatch<S: Store> { /* private fields */ }
Expand description
The primary interface to a Store
.
Implementations§
source§impl<S: Store> Dispatch<S>
impl<S: Store> Dispatch<S>
sourcepub fn global() -> Self
pub fn global() -> Self
Create a new dispatch with the global context (thread local).
This is only available for wasm. For SSR, see the YewduxRoot pattern.
sourcepub fn spawn_future<F, FU>(&self, f: F)
pub fn spawn_future<F, FU>(&self, f: F)
Spawn a future with access to this dispatch.
sourcepub fn future_callback<E, F, FU>(&self, f: F) -> Callback<E>
pub fn future_callback<E, F, FU>(&self, f: F) -> Callback<E>
Create a callback that will spawn a future with access to this dispatch.
sourcepub fn future_callback_with<E, F, FU>(&self, f: F) -> Callback<E>
pub fn future_callback_with<E, F, FU>(&self, f: F) -> Callback<E>
Create a callback that will spawn a future with access to this dispatch and the emitted event.
sourcepub fn subscribe<C: Callable<S>>(self, on_change: C) -> Self
pub fn subscribe<C: Callable<S>>(self, on_change: C) -> Self
Create a dispatch that subscribes to changes in state. Latest state is sent immediately, and on every subsequent change. Automatically unsubscribes when this dispatch is dropped.
use std::rc::Rc;
use yew::prelude::*;
use yewdux::prelude::*;
#[derive(Default, Clone, PartialEq, Eq, Store)]
struct State {
count: u32,
}
struct App {
/// Our local version of state.
state: Rc<State>,
/// Our dispatch. Make sure to keep this, or the subscription will be dropped.
dispatch: Dispatch<State>,
}
enum Msg {
/// Message to receive new state.
State(Rc<State>),
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
let on_change = ctx.link().callback(Msg::State);
let dispatch = Dispatch::global().subscribe(on_change);
Self {
state: dispatch.get(),
dispatch,
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::State(state) => {
self.state = state;
true
}
}
}
/// ...
}
sourcepub fn subscribe_silent<C: Callable<S>>(self, on_change: C) -> Self
pub fn subscribe_silent<C: Callable<S>>(self, on_change: C) -> Self
Create a dispatch that subscribes to changes in state. Similar to Self::subscribe, however state is not sent immediately. Automatically unsubscribes when this dispatch is dropped.
sourcepub fn apply<R: Reducer<S>>(&self, reducer: R)
pub fn apply<R: Reducer<S>>(&self, reducer: R)
Apply a Reducer
immediately.
#[derive(Default, Clone, PartialEq, Eq, Store)]
struct State {
count: u32,
}
struct AddOne;
impl Reducer<State> for AddOne {
fn apply(self, state: Rc<State>) -> Rc<State> {
State {
count: state.count + 1,
}
.into()
}
}
let dispatch = Dispatch::<State>::global();
dispatch.apply(AddOne);
sourcepub fn apply_callback<E, M, F>(&self, f: F) -> Callback<E>
pub fn apply_callback<E, M, F>(&self, f: F) -> Callback<E>
Create a callback that applies a Reducer
.
#[derive(Default, Clone, PartialEq, Eq, Store)]
struct State {
count: u32,
}
struct AddOne;
impl Reducer<State> for AddOne {
fn apply(self, state: Rc<State>) -> Rc<State> {
State {
count: state.count + 1,
}
.into()
}
}
let dispatch = Dispatch::<State>::global();
let onclick = dispatch.apply_callback(|_| AddOne);
html! {
<button {onclick}>{"+1"}</button>
}
sourcepub fn set(&self, val: S)
pub fn set(&self, val: S)
Set state to given value immediately.
let dispatch = Dispatch::<State>::global();
dispatch.set(State { count: 0 });
sourcepub fn set_callback<E, F>(&self, f: F) -> Callback<E>where
F: Fn(E) -> S + 'static,
pub fn set_callback<E, F>(&self, f: F) -> Callback<E>where
F: Fn(E) -> S + 'static,
Set state using value from callback.
let dispatch = use_dispatch::<State>();
let onchange = dispatch.set_callback(|event: Event| {
let value = event.target_unchecked_into::<web_sys::HtmlInputElement>().value();
State { count: value.parse().unwrap() }
});
html! {
<input type="number" placeholder="Enter a number" {onchange} />
}
sourcepub fn reduce<F>(&self, f: F)
pub fn reduce<F>(&self, f: F)
Change state immediately.
let dispatch = Dispatch::<State>::global();
dispatch.reduce(|state| State { count: state.count + 1 }.into());
sourcepub fn reduce_callback<F, E>(&self, f: F) -> Callback<E>
pub fn reduce_callback<F, E>(&self, f: F) -> Callback<E>
Create a callback that changes state.
let dispatch = Dispatch::<State>::global();
let onclick = dispatch.reduce_callback(|state| State { count: state.count + 1 }.into());
html! {
<button {onclick}>{"+1"}</button>
}
sourcepub fn reduce_callback_with<F, E>(&self, f: F) -> Callback<E>
pub fn reduce_callback_with<F, E>(&self, f: F) -> Callback<E>
Similar to Self::reduce_callback but also provides the fired event.
let dispatch = Dispatch::<State>::global();
let onchange = dispatch.reduce_callback_with(|state, event: Event| {
let value = event.target_unchecked_into::<web_sys::HtmlInputElement>().value();
State {
count: value.parse().unwrap()
}
.into()
});
html! {
<input type="number" placeholder="Enter a number" {onchange} />
}
sourcepub fn reduce_mut<F, R>(&self, f: F) -> R
pub fn reduce_mut<F, R>(&self, f: F) -> R
Mutate state with given function.
let dispatch = Dispatch::<State>::global();
dispatch.reduce_mut(|state| state.count += 1);
sourcepub fn reduce_mut_callback<F, R, E>(&self, f: F) -> Callback<E>
pub fn reduce_mut_callback<F, R, E>(&self, f: F) -> Callback<E>
Like Self::reduce_mut but from a callback.
let dispatch = Dispatch::<State>::global();
let onclick = dispatch.reduce_mut_callback(|s| s.count += 1);
html! {
<button {onclick}>{"+1"}</button>
}
sourcepub fn reduce_mut_callback_with<F, R, E>(&self, f: F) -> Callback<E>
pub fn reduce_mut_callback_with<F, R, E>(&self, f: F) -> Callback<E>
Similar to Self::reduce_mut_callback but also provides the fired event.
let dispatch = Dispatch::<State>::global();
let onchange = dispatch.reduce_mut_callback_with(|state, event: Event| {
let value = event.target_unchecked_into::<web_sys::HtmlInputElement>().value();
state.count = value.parse().unwrap();
});
html! {
<input type="number" placeholder="Enter a number" {onchange} />
}
Trait Implementations§
Auto Trait Implementations§
impl<S> !RefUnwindSafe for Dispatch<S>
impl<S> !Send for Dispatch<S>
impl<S> !Sync for Dispatch<S>
impl<S> Unpin for Dispatch<S>
impl<S> !UnwindSafe for Dispatch<S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self
to a value of a Properties
struct.source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self
to a value of a Properties
struct.