17
ANGULAR 1.X STATE MANAGEMENT Adam Klein

Managing state in Angular 1.x with Redux

  • Upload
    500tech

  • View
    1.417

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Managing state in Angular 1.x with Redux

ANGULAR 1.X STATE MANAGEMENT

Adam Klein

Page 2: Managing state in Angular 1.x with Redux

Adam Klein

• Developing software for past 16 years • I love sharing knowledge • I used to have a Jewfro

CTO @ 500Tech

Page 3: Managing state in Angular 1.x with Redux

“A framework takes away the fun of creating our own infrastructure.

But, it adds the fun of complaining about how crappy the framework is, and how better we would have done it ourselves.”

My Quote of the Day

Page 4: Managing state in Angular 1.x with Redux

WE NEED APP-WIDE STATE MANAGEMENT

Page 5: Managing state in Angular 1.x with Redux

Redux on one leg

state

ui component

middleware

selector

action

reducer

Page 6: Managing state in Angular 1.x with Redux

Advantages of ReduxApplication wide State

Decoupled from component structure

Order and Conventions

1 way data flow

Testability

Immutable state

Better change detection

Save previous states

Cache selectors

Page 7: Managing state in Angular 1.x with Redux

Angular 1.X + Redux? Really?!

Page 8: Managing state in Angular 1.x with Redux

Yes! Really!!

Page 9: Managing state in Angular 1.x with Redux

Advantages of ReduxApplication wide State

Decoupled from component structure

Order and Conventions

1 way data flow

Testability

Immutable state

Better change detection

Save previous states

Cache selectors

Page 10: Managing state in Angular 1.x with Redux

Angular 1.5 + Immutable = Minor performance gains

$scope.$watch shallow

Using $onChanges

Selectors can still use Immutable power

Page 11: Managing state in Angular 1.x with Redux

2 way data bindingAvoid 2 way binding directly to state

Beware of canonical bindings (e.g. ‘user.name’)

Beware of ‘=‘ directive bindings

Use Immutable or SeamlessImmutable

• Use ‘track by’ for ng-repeat (angular adds $$hash)

• Some 3rd party libraries might need the non-immutable version

Page 12: Managing state in Angular 1.x with Redux

https://github.com/500tech/ng-redux-example

Page 13: Managing state in Angular 1.x with Redux

TipsMiddlewares can use DI (like async actions)

Reducers / Selectors don’t use DI

$scope.$apply after changing state from redux dev tools

Page 14: Managing state in Angular 1.x with Redux

ALTERNATIVE: state service

Page 15: Managing state in Angular 1.x with Redux

A state serviceStraightforward

Independent of component hierarchy

Custom solution

Direct bindings

Use setters and getters

Option: use RX for subscribing to change

Page 16: Managing state in Angular 1.x with Redux

https://github.com/500tech/angular-state-example

Page 17: Managing state in Angular 1.x with Redux

Read our blog:http://blog.500tech.com

Thank [email protected]

slideshare.net/500tech