pub fn use_reducer_with_init<Action: 'static, Reducer, State: 'static, InitialState, InitFn>(
    reducer: Reducer,
    initial_state: InitialState,
    init: InitFn,
) -> (Rc<State>, Rc<impl Fn(Action)>)
where
    Reducer: Fn(Rc<State>, Action) -> State + 'static,
    InitFn: Fn(InitialState) -> State,
{
    struct UseReducerState<State> {
        current_state: Rc<State>,
    }
    impl<T> Hook for UseReducerState<T> {}
    let init = Box::new(init);
    let reducer = Rc::new(reducer);
    use_hook(
        |internal_hook_change: &mut UseReducerState<State>, hook_callback| {
            (
                internal_hook_change.current_state.clone(),
                Rc::new(move |action: Action| {
                    let reducer = reducer.clone();
                    hook_callback(
                        move |internal_hook_change: &mut UseReducerState<State>| {
                            internal_hook_change.current_state = Rc::new((reducer)(
                                internal_hook_change.current_state.clone(),
                                action,
                            ));
                            true
                        },
                        false, // run pre render
                    );
                }),
            )
        },
        move || UseReducerState {
            current_state: Rc::new(init(initial_state)),
        },
    )
}
/// [`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_functional::{function_component, use_reducer_with_init};
/// # use yew::prelude::*;
/// # use std::rc::Rc;
/// #
/// #[function_component(UseReducerWithInit)]
/// fn reducer_with_init() -> Html {
///     struct CounterState {
///         counter: i32,
///     }
///     let (counter, dispatch) = 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 |_| 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<Action: 'static, Reducer, State: 'static, InitialState, InitFn>(
    reducer: Reducer,
    initial_state: InitialState,
    init: InitFn,
) -> (Rc<State>, Rc<impl Fn(Action)>)
where
    Reducer: Fn(Rc<State>, Action) -> State + 'static,
    InitFn: Fn(InitialState) -> State,
{
    struct UseReducerState<State> {
        current_state: Rc<State>,
    }
    impl<T> Hook for UseReducerState<T> {}
    let init = Box::new(init);
    let reducer = Rc::new(reducer);
    use_hook(
        |internal_hook_change: &mut UseReducerState<State>, hook_callback| {
            (
                internal_hook_change.current_state.clone(),
                Rc::new(move |action: Action| {
                    let reducer = reducer.clone();
                    hook_callback(
                        move |internal_hook_change: &mut UseReducerState<State>| {
                            internal_hook_change.current_state = Rc::new((reducer)(
                                internal_hook_change.current_state.clone(),
                                action,
                            ));
                            true
                        },
                        false, // run pre render
                    );
                }),
            )
        },
        move || UseReducerState {
            current_state: Rc::new(init(initial_state)),
        },
    )
}
pub fn use_reducer_with_init < Action : ' static , Reducer , State : ' static , InitialState , InitFn > ( reducer : Reducer , initial_state : InitialState , init : InitFn , ) -> ( Rc < State > , Rc < impl Fn ( Action ) > ) where Reducer : Fn ( Rc < State > , Action ) -> State + ' static , InitFn : Fn ( InitialState ) -> State , { struct UseReducerState < State > { current_state : Rc < State > , } impl < T > Hook for UseReducerState < T > { } let init = Box :: new ( init ) ; let reducer = Rc :: new ( reducer ) ; use_hook ( | internal_hook_change : & mut UseReducerState < State > , hook_callback | { ( internal_hook_change . current_state . clone ( ) , Rc :: new ( move | action : Action | { let reducer = reducer . clone ( ) ; hook_callback ( move | internal_hook_change : & mut UseReducerState < State > | { internal_hook_change . current_state = Rc :: new ( ( reducer ) ( internal_hook_change . current_state . clone ( ) , action , ) ) ; true } , false , // run pre render ) ; } ) , ) } , move | | UseReducerState { current_state : Rc :: new ( init ( initial_state ) ) , } , ) }
[`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_functional::{function_component, use_reducer_with_init};
None
use yew::prelude::*;
None
use std::rc::rc;
None
[function_component(usereducerwithinit)]
fn reducer_with_init() -> Html {
struct CounterState {
counter: i32,
}
let (counter, dispatch) = 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 dispatch ) = use_reducer_with_init ( |prev : Rc action : i32| CounterState { counter : prev . counter + action } 0 |initial : i32| CounterState { counter : initial + 10 } ) . html . { < > { counter . counter }