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.
§Higher-Order Component Pattern with YewduxRoot
use std::rc::Rc;
use yew::prelude::*;
use yewdux::prelude::*;
#[derive(Default, Clone, PartialEq, Eq, Store)]
struct State {
count: u32,
}
// Props for our struct component
#[derive(Properties, PartialEq, Clone)]
struct CounterProps {
dispatch: Dispatch<State>,
}
// Message type for state updates
enum Msg {
StateChanged(Rc<State>),
}
// Our struct component that uses the state
struct Counter {
state: Rc<State>,
dispatch: Dispatch<State>,
}
impl Component for Counter {
type Message = Msg;
type Properties = CounterProps;
fn create(ctx: &Context<Self>) -> Self {
// Subscribe to state changes
let callback = ctx.link().callback(Msg::StateChanged);
let dispatch = ctx.props().dispatch.clone().subscribe_silent(callback);
Self {
state: dispatch.get(),
dispatch,
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::StateChanged(state) => {
self.state = state;
true
}
}
}
fn view(&self, _ctx: &Context<Self>) -> Html {
let count = self.state.count;
let onclick = self.dispatch.reduce_mut_callback(|s| s.count += 1);
html! {
<>
<h1>{ count }</h1>
<button {onclick}>{"+1"}</button>
</>
}
}
}
// Higher-Order Component (HOC) that accesses the context
#[function_component]
fn CounterHoc() -> Html {
// Use the hook to get the dispatch from context
let dispatch = use_dispatch::<State>();
html! {
<Counter {dispatch} />
}
}
// App component with YewduxRoot for SSR support
#[function_component]
fn App() -> Html {
html! {
<YewduxRoot>
<CounterHoc />
</YewduxRoot>
}
}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()
}
}
// Apply a reducer to update the state
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()
}
}
// Create a callback that will update the state when triggered
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.
// Set the state to a new value
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.
// Transform the current state into a new state
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.
// Create a callback that will transform the state when triggered
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.
// Create a callback that will transform the state using the event data
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.
// Mutate the state in-place
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.
// Create a callback that will mutate the state in-place when triggered
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.
// Create a callback that will mutate the state using event data
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§
Source§impl<S: Store> Default for Dispatch<S>
Available on crate feature doctests or WebAssembly only.
impl<S: Store> Default for Dispatch<S>
doctests or WebAssembly only.Auto Trait Implementations§
impl<S> !Freeze for Dispatch<S>
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> UnsafeUnpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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.