redux data flow with angular

39

Upload: gil-fink

Post on 28-Jan-2018

189 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Redux data flow with angular
Page 2: Redux data flow with angular

REDUX DATA FLOW WITH ANGULAR

Page 3: Redux data flow with angular

SPAs Are Complex

Page 4: Redux data flow with angular

Service

Component

Service

ComponentComponent

Page 5: Redux data flow with angular

Service

Component

Service

ComponentComponent

Component

Component

Service

Service Component

Service

Page 6: Redux data flow with angular

Service

Component

Service

ComponentComponent

Component

Component

Service

Service Component

Service

Service

Component

Service

ComponentComponent

Component

Component

Service

Service Component

Service

Service

Component

Service

ComponentComponent

Component

Component

Service

Service Component

Service

Page 7: Redux data flow with angular

Then you make a small change…

Page 8: Redux data flow with angular
Page 9: Redux data flow with angular

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 10: Redux data flow with angular

Agenda

• The Redux pattern

• Combining Redux and Angular

• ng2-redux library in a nutshell

Page 11: Redux data flow with angular

Known SPA Complexity Solutions

• Command Query Responsibility Segregation (CQRS)

• Event Sourcing

• Flux

• And more

Page 12: Redux data flow with angular

Redux

• Design pattern based on Flux• But it’s not Flux!

• Predictable state container for JavaScript apps

Page 13: Redux data flow with angular

Redux Principles

1. Single source of truth

Page 14: Redux data flow with angular

Single Source of Truth

• The application state is stored in an object tree within a single store

let state = store.getState();

Page 15: Redux data flow with angular

Redux Principles

1. Single source of truth

2. State is read-only

Page 16: Redux data flow with angular

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 17: Redux data flow with angular

Redux Principles

1. Single source of truth

2. State is read-only

3. Changes are made with pure functions

Page 18: Redux data flow with angular

Changes Are Made with Pure Functions

• To specify how the state is transformed by actions, you write a pure function

function groceryItemReducer(state, action) {switch (action.type) {

case 'ADD_GROCERY_ITEM':return object.assign({}, state, {

groceryItems: [action.item,…state.groceryItems

]};

default:return state;

}}

Page 19: Redux data flow with angular

Let’s drill down

Page 20: Redux data flow with angular

Redux Actors

Action Creators

View

ReducersStore

Page 21: Redux data flow with angular

Actions

• Plain JavaScript objects

• Include• Type – action type

• Payload – the change that should occur

let action = {type: 'ADD_GROCERY_ITEM',item: { productToBuy: 'Milk' }

};

Action Creators

Page 22: Redux data flow with angular

Action Creators

• Functions that return actions

const ADD_GROCERY_ITEM = 'ADD_GROCERY_ITEM';

function addGroceryItem(item) {return {

type: ADD_GROCERY_ITEM,item: item

};}

Action Creators

Page 23: Redux data flow with angular

Reducers

• Change the state in response to things that happened in the application

• Pure functions

function groceryItemReducer(state, action) {switch (action.type) {

case ADD_GROCERY_ITEM:return object.assign({}, state, {

groceryItems: [action.item,…state.groceryItems

]};

default:return state;

}}

Reducers

Page 24: Redux data flow with angular

Store

• Only one store object

• Store data accessed through getState function

• Updates only in response to dispatching an action

import {createStore} from ‘redux’;import {groceryItemReducer} from ‘./reducers/groceryItemReducer’;

const store = createStore(groceryItemReducer);

Store

Page 25: Redux data flow with angular

Redux Data Flow

Action Creators

View

ReducersStore

interaction (click, mouse etc.)

dispatch(action) (previous state, action)

(new state)(current state)

Page 26: Redux data flow with angular

Redux Data Flow – Cont.

Action Creators

View

ReducersStore

interaction (click, mouse etc.)

dispatch(action) (previous state, action)

(new state)(current state)

Outside Action (push, Ajax callback

and etc.)

Page 27: Redux data flow with angular

Redux Library API

• Redux is a small and compact library:• createStore(reducer)

• combineReducers(reducers)

• applyMiddleware(middlewares)

• compose(functions)

• bindActionCreators(creators, dispatch)

• Store API:• store.dispatch(action)

• store.getState()

• store.subscribe(listener)

Page 28: Redux data flow with angular

DEMO: REDUX IN ACTION

Page 29: Redux data flow with angular

Redux Middleware

• Extension point between dispatching an action and the reducer

• Middleware examples:• Logging

• Async operations

• Routing

const middleware = store => next => action => {// do something before dispatcher

let result = next(action);// do something after dispatcherreturn result;

}

const store = createStore(reducer, applyMiddleware(middleware));

Page 30: Redux data flow with angular

DEMO: REDUX MIDDLEWARE

Page 31: Redux data flow with angular

WHAT ABOUT ANGULAR?

Page 32: Redux data flow with angular

Angular and Redux

• Redux can be applied in Angular oriented applications• It’s a data flow pattern

• Popular Angular & Redux libraries:• ng2-redux

• ngrx/store

Page 33: Redux data flow with angular

NG2-REDUXhttps://github.com/angular-redux/ng2-redux

npm install --save ng2-redux

Page 34: Redux data flow with angular

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 35: Redux data flow with angular

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 36: Redux data flow with angular

DEMO: REDUX + ANGULAR

Page 37: Redux data flow with angular

Summary

• Redux • Is a data flow design pattern

• Reduces complexity when building big SPAs

• Can be applied in Angular apps

Page 38: Redux data flow with angular

Resources

• Redux:https://github.com/reactjs/redux

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

• ngrx/store:https://github.com/ngrx/store

• My Blog – http://www.gilfink.net

• Follow me on Twitter – @gilfink

Page 39: Redux data flow with angular

Thank You!