Function sycamore_reactive::create_reducer [−][src]
pub fn create_reducer<F, Out, Msg>(
initial: Out,
reduce: F
) -> (ReadSignal<Out>, Rc<impl Fn(Msg)>) where
F: Fn(&Out, Msg) -> Out,
Expand description
An alternative to Signal::new
that uses a reducer to get the next value.
It uses a reducer function that takes the previous value and a message and returns the next value.
Returns a ReadSignal
and a dispatch function to send messages to the reducer.
Params
initial
- The initial value of the state.reducer
- A function that takes the previous value and a message and returns the next value.
Example
use sycamore_reactive::*;
enum Msg {
Increment,
Decrement,
}
let (state, dispatch) = create_reducer(0, |state, msg: Msg| match msg {
Msg::Increment => *state + 1,
Msg::Decrement => *state - 1,
});
assert_eq!(*state.get(), 0);
dispatch(Msg::Increment);
assert_eq!(*state.get(), 1);
dispatch(Msg::Decrement);
assert_eq!(*state.get(), 0);