pub fn create_reducer<U, Msg>(
        &'a self,
        initial: U,
        reduce: impl Fn(&U, Msg) -> U + 'a,
    ) -> (&'a ReadSignal<U>, impl Fn(Msg) + 'a) {
        let memo = self.create_signal(initial);

        let dispatcher = move |msg| {
            memo.set(reduce(&memo.get_untracked(), msg));
        };

        (&*memo, dispatcher)
    }
/// An alternative to [`create_signal`](Self::create_signal) 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,
/// }
///
/// # create_scope_immediate(|ctx| {
/// let (state, dispatch) = ctx.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);
/// # });
/// ```
An alternative to [`create_signal`](Self::create_signal) 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.
An alternative to [ ` create_signal ` ] ( Self :: create_signal ) 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 .
pub fn create_reducer<U, Msg>(
        &'a self,
        initial: U,
        reduce: impl Fn(&U, Msg) -> U + 'a,
    ) -> (&'a ReadSignal<U>, impl Fn(Msg) + 'a) {
        let memo = self.create_signal(initial);
        let dispatcher = move |msg| {
            memo.set(reduce(&memo.get_untracked(), msg));
        };
        (&*memo, dispatcher)
    }
pub fn create_reducer < U , Msg > ( & ' a self , initial : U , reduce : impl Fn ( & U , Msg ) -> U + ' a , ) -> ( & ' a ReadSignal < U > , impl Fn ( Msg ) + ' a ) { let memo = self . create_signal ( initial ) ; let dispatcher = move | msg | { memo . set ( reduce ( & memo . get_untracked ( ) , msg ) ) ; } ; ( & * memo , dispatcher ) }
An alternative to [`create_signal`](Self::create_signal) that uses a
reducer to get the next value.
An alternative to [ ` create_signal ` ] ( Self :: create_signal ) that uses a reducer to get the next value .
&'a self
initial
U
reduce
impl Fn(&U, Msg) -> U + 'a
&'a self
initial
U
reduce
impl Fn(&U, Msg) -> U + 'a
params
`initial` - The initial value of the state.
`reducer` - A function that takes the previous value and a message and
returns the next value.
` initial ` - The initial value of the state . ` reducer ` - A function that takes the previous value and a message and returns the next value .
example
None
use sycamore_reactive::*;
enum Msg {
Increment,
Decrement,
}
enum Msg { Increment Decrement }
create_scope_immediate(|ctx| {
let (state, dispatch) = ctx.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).
let ( state dispatch ) = ctx . 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 ) .
});
None