Successfully reported this slideshow.
Your SlideShare is downloading. ×

Redux data flow with angular

Ad

REDUX DATA FLOW WITH
ANGULAR

Ad

SPAs Are Complex

Ad

Service
Component
Service
ComponentComponent

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Upcoming SlideShare
Redux data flow with angular
Redux data flow with angular
Loading in …3
×

Check these out next

1 of 39 Ad
1 of 39 Ad

More Related Content

Redux data flow with angular

  1. 1. REDUX DATA FLOW WITH ANGULAR
  2. 2. SPAs Are Complex
  3. 3. Service Component Service ComponentComponent
  4. 4. Service Component Service ComponentComponent Component Component Service Service Component Service
  5. 5. 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
  6. 6. Then you make a small change…
  7. 7. 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
  8. 8. Agenda • The Redux pattern • Combining Redux and Angular • ng2-redux library in a nutshell
  9. 9. Known SPA Complexity Solutions • Command Query Responsibility Segregation (CQRS) • Event Sourcing • Flux • And more
  10. 10. Redux • Design pattern based on Flux • But it’s not Flux! • Predictable state container for JavaScript apps
  11. 11. Redux Principles 1. Single source of truth
  12. 12. Single Source of Truth • The application state is stored in an object tree within a single store let state = store.getState();
  13. 13. Redux Principles 1. Single source of truth 2. State is read-only
  14. 14. 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 });
  15. 15. Redux Principles 1. Single source of truth 2. State is read-only 3. Changes are made with pure functions
  16. 16. 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; } }
  17. 17. Let’s drill down
  18. 18. Redux Actors Action Creators View ReducersStore
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. Redux Data Flow Action Creators View ReducersStore interaction (click, mouse etc.) dispatch(action) (previous state, action) (new state) (current state)
  24. 24. 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.)
  25. 25. 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)
  26. 26. DEMO: REDUX IN ACTION
  27. 27. 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 dispatcher return result; } const store = createStore(reducer, applyMiddleware(middleware));
  28. 28. DEMO: REDUX MIDDLEWARE
  29. 29. WHAT ABOUT ANGULAR?
  30. 30. 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
  31. 31. NG2-REDUX https://github.com/angular-redux/ng2-redux npm install --save ng2-redux
  32. 32. 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); } }
  33. 33. 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}); } }
  34. 34. DEMO: REDUX + ANGULAR
  35. 35. Summary • Redux • Is a data flow design pattern • Reduces complexity when building big SPAs • Can be applied in Angular apps
  36. 36. 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
  37. 37. Thank You!

×