Reducer Hook

It is how useReducer hook would look in Angular.

⚡️Reducer Hook in Template

An example in COMPONENT.component.ts.

@Component({
  selector: 'app-use-reducer',
  templateUrl: './use-reducer.component.html'
})
export class UseReducerComponent implements OnInit {

  initialAction = {
    type: 'reset',
    payload: 100
  };

  defaultState = {
    count: 0
  };

  reducer = (state, action) => {
    switch (action.type) {
      case 'reset':
        return { count: action.payload };
      case 'increment':
        return { count: state.count + 1 };
      case 'decrement':
        return { count: state.count - 1 };
      default:
        // A reducer must always return a valid state.
        // Alternatively you can throw an error if an invalid action is dispatched.
        return state;
    }
  }

}

An example in COMPONENT.template.html.

⚡️Reducer Hook with Ivy

An example in COMPONENT.component.ts.

Last updated