NgRx v8を使ってみよう

8bc941b1868dfc680dd1e11a797ca385?s=47 puku0x
September 11, 2019

NgRx v8を使ってみよう

8bc941b1868dfc680dd1e11a797ca385?s=128

puku0x

September 11, 2019
Tweet

Transcript

  1. NgRx v8を使ってみよう Angularもくもく会 in Fukuoka #9

  2. Noriyuki Shinpuku ng-fukuoka organizer VEGA corporation Co., Ltd. @puku0x

  3. Angular

  4. Full-fledged & opinionated Angular Protractor Forms PWA Augury Language Services

    Router Elements CDK Universal Karma Labs Compiler i18n Http Material Animations CLI
  5. State management?

  6. RxJS

  7. Basic state management • BehaviorSubject • ReplaySubject ◦ No initial

    value ◦ No current value getter
  8. BehaviorSubject import { Injectable } from '@angular/core'; import { BehaviorSubject

    } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class CouterService { private counter = new BehaviorSubject(0); // Initial value = 0 get counter$() { return this.counter.asObservable(); } increment() { const value = this.counter.getValue(); this.counter.next(value + 1); } }
  9. ReplaySubject import { Injectable } from '@angular/core'; import { BehaviorSubject

    } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class CouterService { private counter = new ReplaySubject(1); // Buffer size = 1 get counter$() { return this.counter.asObservable(); } setCounter(value: number) { this.counter.next(value); } }
  10. Complex state management?

  11. None
  12. NgRx • RxJS powered Redux for Angular • Type-safe and

    performant • Community driven
  13. None
  14. Does it have many boilerplates?

  15. NgRx v8 has less boilerplates!

  16. https://blog.angularindepth.com/ngrx-action-creators-redesigned-d396960e46da

  17. Action creator factory import { createAction, props } from '@ngrx/store';

    export const increment = createAction( '[Counter] Increment' ); export const setValue = createAction( '[Counter] Set value, props<{ value: number }>() );
  18. Reducer creator factory import { createReducer, on } from '@ngrx/store';

    import { increment, setValue } from './counter.actions'; export const initialState = 0; const counterReducer = createReducer(initialState, on(increment, state => state + 1), // No more switch-case! on(setValue, (state, { value }) => value) );
  19. Effect creator factory import { Actions, ofType, createEffect } from

    '@ngrx/effects'; import { tap } from 'rxjs/operators'; import { setValue } from '../actions'; @Injectable() export class CounterEffects { constructor(private actions$: Actionn) {} setValue$ = createEffect(() => // No more @Effect decorator! this.actions$.pipe( ofType(setValue), tap(({ value }) => console.log(`${value}`)) ), { dispatch: false } );
  20. Creator factories make NgRx simple!

  21. Recap • NgRx provides Redux for Angular • Less boilerplate

    with creator factories • Try it now! https://ngrx.io/ https://stackblitz.com/edit/ngrx-todo-v8
  22. Thank you! @puku0x Noriyuki Shinpuku