useReducer<TState, TAction, TInit> function

ReducerHook<TState, TAction, TInit> useReducer<TState, TAction, TInit>(
  1. TState reducer(
    1. TState state,
    2. TAction action
    ),
  2. TState initialState
)

Initializes state of a uiFunction component to initialState and creates ReducerHook.dispatch method.

Example:

Map reducer(Map state, Map action) {
  switch (action['type']) {
    case 'increment':
      return {...state, 'count': state['count'] + 1};
    case 'decrement':
      return {...state, 'count': state['count'] - 1};
    default:
      return state;
  }
}

UiFactory<UseReducerExampleProps> UseReducerExample = uiFunction(
  (props) {
    final state = useReducer(reducer, {'count': 0});

    return Fragment()(
      state.state['count'],
      (Dom.button()
        ..onClick = (_) => state.dispatch({'type': 'increment'})
      )('+'),
      (Dom.button()
        ..onClick = (_) => state.dispatch({'type': 'decrement'})
      )('-'),
    );
  },
  _$UseReducerExampleConfig, // ignore: undefined_identifier
);

Learn more: reactjs.org/docs/hooks-reference.html#usereducer.

Implementation

ReducerHook<TState, TAction, TInit> useReducer<TState, TAction, TInit>(
    TState Function(TState state, TAction action) reducer, TState initialState) =>
    react_hooks.useReducer(reducer, initialState);