pub type ReducerFn<State, Action, Event, Effect> = fn(&Rc<State>, &Action) -> ReducerResult<State, Event, Effect>;Expand description
A wrapper for a function that implements the Reducer trait.
ยงExample
use reactive_state::{ReducerFn, ReducerResult, Reducer};
use std::rc::Rc;
let reducer: ReducerFn<MyState, MyAction, MyEvent, MyEffect> = |state, action| {
let mut events = Vec::new();
let new_state = match action {
MyAction::SomeAction => {
// create a new state to mutate and return
let mut new_state = MyState::clone(state);
new_state.variable = true;
// An event needs to be produced to notify
// subscribers that the state has changed.
events.push(MyEvent::SomeEvent);
Rc::new(new_state)
}
};
ReducerResult {
state: new_state,
events: events,
effects: vec![],
}
};
let state1 = Rc::new(MyState {
variable: false
});
let result = reducer.reduce(&state1, &MyAction::SomeAction);
let state2 = &result.state;
assert_eq!(false, state1.variable);
assert_eq!(true, state2.variable);For a more comprehensive example of how reducers are used in the context of the entire system, see reactive_state.