pub fn use_reducer_with_init<Reducer, Action, State, InitialState, InitFn>(
    reducer: Reducer,
    initial_state: InitialState,
    init: InitFn,
) -> UseReducerHandle<State, Action>
where
    Reducer: Fn(Rc<State>, Action) -> State + 'static,
    Action: 'static,
    State: 'static,
    InitialState: 'static,
    InitFn: Fn(InitialState) -> State,
{
    let init = Box::new(init);
    let reducer = Rc::new(reducer);
    use_hook(
        move || UseReducer {
            current_state: Rc::new(init(initial_state)),
        },
        |s, updater| {
            let setter: Rc<dyn Fn(Action)> = Rc::new(move |action: Action| {
                let reducer = reducer.clone();
                // We call the callback, consumer the updater
                // Required to put the type annotations on Self so the method knows how to downcast
                updater.callback(move |state: &mut UseReducer<State>| {
                    let new_state = reducer(state.current_state.clone(), action);
                    state.current_state = Rc::new(new_state);
                    true
                });
            });

            UseReducerHandle {
                value: Rc::clone(&s.current_state),
                setter,
            }
        },
        |_| {},
    )
}
/// [`use_reducer`] but with init argument.
///
/// This is useful for lazy initialization where it is beneficial not to perform expensive
/// computation up-front
///
/// # Example
/// ```rust
/// # use yew::prelude::*;
/// # use std::rc::Rc;
/// #
/// #[function_component(UseReducerWithInit)]
/// fn reducer_with_init() -> Html {
///     struct CounterState {
///         counter: i32,
///     }
///     let counter = use_reducer_with_init(
///         |prev: Rc<CounterState>, action: i32| CounterState {
///             counter: prev.counter + action,
///         },
///         0,
///         |initial: i32| CounterState {
///             counter: initial + 10,
///         },
///     );
///
///     html! {
///         <>
///             <div id="result">{counter.counter}</div>
///
///             <button onclick={Callback::from(move |_| counter.dispatch(10))}>{"Increment by 10"}</button>
///         </>
///     }
/// }
/// ```
[`use_reducer`] but with init argument.
This is useful for lazy initialization where it is beneficial not to perform expensive
computation up-front
[ ` use_reducer ` ] but with init argument . This is useful for lazy initialization where it is beneficial not to perform expensive computation up - front
pub fn use_reducer_with_init<Reducer, Action, State, InitialState, InitFn>(
    reducer: Reducer,
    initial_state: InitialState,
    init: InitFn,
) -> UseReducerHandle<State, Action>
where
    Reducer: Fn(Rc<State>, Action) -> State + 'static,
    Action: 'static,
    State: 'static,
    InitialState: 'static,
    InitFn: Fn(InitialState) -> State,
{
    let init = Box::new(init);
    let reducer = Rc::new(reducer);
    use_hook(
        move || UseReducer {
            current_state: Rc::new(init(initial_state)),
        },
        |s, updater| {
            let setter: Rc<dyn Fn(Action)> = Rc::new(move |action: Action| {
                let reducer = reducer.clone();
                // We call the callback, consumer the updater
                // Required to put the type annotations on Self so the method knows how to downcast
                updater.callback(move |state: &mut UseReducer<State>| {
                    let new_state = reducer(state.current_state.clone(), action);
                    state.current_state = Rc::new(new_state);
                    true
                });
            });
            UseReducerHandle {
                value: Rc::clone(&s.current_state),
                setter,
            }
        },
        |_| {},
    )
}
pub fn use_reducer_with_init < Reducer , Action , State , InitialState , InitFn > ( reducer : Reducer , initial_state : InitialState , init : InitFn , ) -> UseReducerHandle < State , Action > where Reducer : Fn ( Rc < State > , Action ) -> State + ' static , Action : ' static , State : ' static , InitialState : ' static , InitFn : Fn ( InitialState ) -> State , { let init = Box :: new ( init ) ; let reducer = Rc :: new ( reducer ) ; use_hook ( move | | UseReducer { current_state : Rc :: new ( init ( initial_state ) ) , } , | s , updater | { let setter : Rc < dyn Fn ( Action ) > = Rc :: new ( move | action : Action | { let reducer = reducer . clone ( ) ; // We call the callback, consumer the updater // Required to put the type annotations on Self so the method knows how to downcast updater . callback ( move | state : & mut UseReducer < State > | { let new_state = reducer ( state . current_state . clone ( ) , action ) ; state . current_state = Rc :: new ( new_state ) ; true } ) ; } ) ; UseReducerHandle { value : Rc :: clone ( & s . current_state ) , setter , } } , | _ | { } , ) }
[`use_reducer`] but with init argument.
[ ` use_reducer ` ] but with init argument .
reducer
Reducer
initial_state
InitialState
init
InitFn
reducer
Reducer
initial_state
InitialState
init
InitFn
example
None
use yew::prelude::*;
None
use std::rc::rc;
None
[function_component(usereducerwithinit)]
fn reducer_with_init() -> Html {
struct CounterState {
counter: i32,
}
let counter = use_reducer_with_init(
|prev: Rc, action: i32| CounterState {
counter: prev.counter + action,
},
0,
|initial: i32| CounterState {
counter: initial + 10,
},
).

html. {
<>
{counter.counter}


fn reducer_with_init () - > Html { struct CounterState { counter : i32 } let counter = use_reducer_with_init ( |prev : Rc action : i32| CounterState { counter : prev . counter + action } 0 |initial : i32| CounterState { counter : initial + 10 } ) . html . { < > { counter . counter }