redux data flow with angular 2

31
REDUX DATA FLOW WITH ANGULAR 2 Gil Fink CEO and Senior Consultant, sparXys @gilfink

Upload: gil-fink

Post on 15-Apr-2017

296 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Redux data flow with angular 2

REDUX DATA FLOW WITH

ANGULAR 2

Gil Fink

CEO and Senior Consultant, sparXys

@gilfink

Page 2: Redux data flow with angular 2

SPAs Are Complex

Page 3: Redux data flow with angular 2

Service

Component

Service

Component Component

Page 4: Redux data flow with angular 2

Service

Component

Service

Component Component

Component

Component

Service

Service Component

Service

Page 5: Redux data flow with angular 2

5

Service

Component

Service

Component Component

Component

Component

Service

Service Component

Service

Service

Component

Service

Component Component

Component

Component

Service

Service Component

Service

Service

Component

Service

Component Component

Component

Component

Service

Service Component

Service

Page 6: Redux data flow with angular 2

Then you make a small change…

Page 7: Redux data flow with angular 2
Page 8: Redux data flow with angular 2

About Me • sparXys CEO and senior consultant

• Microsoft MVP in the last 8 years

• Pro Single Page Application Development (Apress)

co-author

• 4 Microsoft Official Courses (MOCs) co-author

• GDG Rishon and AngularUP co-organizer

Page 9: Redux data flow with angular 2

Agenda • The Redux pattern

• Combining Redux and Angular 2

• ng2-redux library in a nutshell

Page 10: Redux data flow with angular 2

Known SPA Complexity Solutions

• Command Query Responsibility Segregation (CQRS)

• Event Sourcing

• Flux

• And more

Page 11: Redux data flow with angular 2

Redux • Design pattern based on Flux

o But it’s not Flux!

• Predictable state container for JavaScript apps

Page 12: Redux data flow with angular 2

Redux Principles 1. Single source of truth

Page 13: Redux data flow with angular 2

Single Source of Truth • The application state is stored in an object tree

within a single store

let state = store.getState();

Page 14: Redux data flow with angular 2

Redux Principles 1. Single source of truth

2. State is read-only

Page 15: Redux data flow with angular 2

State is Read-Only • The only way to mutate the state is to emit an

action describing what happened

store.dispatch({ type: 'ADD_GROCERY_ITEM', item: { productToBuy: 'Milk' } });

store.dispatch({ type: 'REMOVE_GROCERY_ITEM', index: 3 });

Page 16: Redux data flow with angular 2

Redux Principles 1. Single source of truth

2. State is read-only

3. Changes are made with pure functions

Page 17: Redux data flow with angular 2

Changes Are Made with Pure Functions

• To specify how the state is transformed by actions,

you write a pure function o Called reducer

function groceryItemReducer(state, action) { switch (action.type) { case 'ADD_GROCERY_ITEM': return object.assign({}, state, { groceryItems: [ action.item, …state.groceryItems ] }; default: return state; } }

Page 18: Redux data flow with angular 2

Let’s drill down

Page 19: Redux data flow with angular 2

Redux Data Flow

Action Creators

View

Reducers Store

interaction (click, mouse etc.)

dispatch(action) (previous state, action)

(new state) (current state)

Page 20: Redux data flow with angular 2

Redux Data Flow – Cont.

Action Creators

View

Reducers Store

interaction (click, mouse etc.)

dispatch(action) (previous state, action)

(new state) (current state)

Outside Action (push, Ajax

callback and etc.)

Page 21: Redux data flow with angular 2

Redux Library API • Redux is a small and compact library:

o createStore(reducer)

o combineReducers(reducers)

o applyMiddleware(middlewares)

o compose(functions)

o bindActionCreators(creators, dispatch)

o Store API:

• store.dispatch(action)

• store.getState()

• store.subscribe(listener)

Page 22: Redux data flow with angular 2

Demo Redux in action

Page 23: Redux data flow with angular 2

What about Angular?

Page 24: Redux data flow with angular 2

Angular and Redux • Redux can be applied in Angular oriented

applications o It’s a data flow pattern

• Popular Angular 2 & Redux libraries: o ngrx/store

o ng2-redux

Page 25: Redux data flow with angular 2

ng2-redux https://github.com/angular-redux/ng2-redux

Page 26: Redux data flow with angular 2

ng2-redux - Setup • Add the module

• Create the store and provide it in the main module

import { NgReduxModule, NgRedux } from 'ng2-redux';

@NgModule({ imports: [ /* ... */, NgReduxModule ] })

class AppModule { constructor(ngRedux: NgRedux<IAppState>) { ngRedux.provideStore(store); } }

Page 27: Redux data flow with angular 2

ng2-redux - Usage • Use the @select decorator and dispatch function:

class App { @select() counter: Observable<number>;

constructor(private ngRedux: NgRedux<IAppState>) {}

increment() { this.ngRedux.dispatch({ type: INCREMENT}); } }

Page 28: Redux data flow with angular 2

Demo Simple Redux + Angular 2 app

Page 29: Redux data flow with angular 2

Summary • Redux

o Is a data flow design pattern

o Reduces complexity when building big SPAs

o Can be applied in Angular apps

Page 31: Redux data flow with angular 2

Thank You!