Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NgRx v8を使ってみよう

puku0x
September 11, 2019

NgRx v8を使ってみよう

puku0x

September 11, 2019
Tweet

More Decks by puku0x

Other Decks in Technology

Transcript

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

    Router Elements CDK Universal Karma Labs Compiler i18n Http Material Animations CLI
  2. 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); } }
  3. 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); } }
  4. 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 }>() );
  5. 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) );
  6. 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 } );
  7. Recap • NgRx provides Redux for Angular • Less boilerplate

    with creator factories • Try it now! https://ngrx.io/ https://stackblitz.com/edit/ngrx-todo-v8