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

Angular and the Single Immutable State Tree

Ciro Nunes
February 18, 2016

Angular and the Single Immutable State Tree

Ciro Nunes

February 18, 2016
Tweet

More Decks by Ciro Nunes

Other Decks in Programming

Transcript

  1. 1. Other components can change it not propagating the change

    PROBLEMS WITH MUTABLE OBJECTS 2. The framework needs to be conservative
  2. my-person.component.ts @Component({+ ++selector:+'my3person',+ ++template:+`+ +++{{+person.name+}}+lives+at+{{+address.street+}},+{{+address.city+}}+{{+zipcode+}}+ ++`,+ ++changeDetection:+ChangeDetectionStrategy.OnPush+ })+ export+class+MyPerson+{+ ++@Input()+person:+{+name:+string+};+

    ++@Input()+address:+{+city:+string,+street:+string+}+ ++zipCode:+number;+ ++ngOnChanges(inputChanges)+{+ ++++if+(inputChanges.address)+{+ ++++++this.zipCode+=+this.zipCodes(this.address);+ ++++}+ ++}+ }
  3. 1. Tracking changes made easy ADVANTAGES OF IMMUTABLE OBJECTS 2.

    Able to skip checks, results in better performance
  4. 1. Unidirectional data flow THE SINGLE IMMUTABLE STATE TREE 2.

    Changes are made using pure functions (reducers)
  5. 1. Always produce the same output given the same input

    PURE FUNCTIONS 2. Don’t produce side-effects (e.g: mutate state)
  6. Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW JS objects

    Pure functions Application state subscribe dispatch
  7. Mutable objects are bad for modelling application state IN SUMMARY

    Immutable objects makes it easy to track changes
  8. Mutable objects are bad for modelling application state IN SUMMARY

    Immutable objects makes it easy to track changes Angular 2 CD mechanism makes it even faster
  9. Mutable objects are bad for modelling application state IN SUMMARY

    Immutable objects makes it easy to track changes Angular 2 CD mechanism makes it even faster Redux is awesome, but there are other great options