REDUX & ANGULAR UP & RUNNING NIR KAUFMAN
AGENDA what is redux? how to use redux? where Angular fits in? where to go from here?
WHAT IS REDUX
REDUX IS A DESIGN PATTERN for managing a complex, ever changing state in a challenging modern single page applications
SINGLE SOURCE OF TRUTH the state of your whole application is stored within a single store
THE STATE IS READ ONLY the only way to mutate the state is to emit an action describing what happened
PURE FUNCTIONS to specify how the state is transformed by actions, you write a pure function.
PURE* FUNCTION ACTION NEXT STATE * No side-effects reducer
const currentState = 0;
 const action = {type: 'INCREASE'};
 
 function reducer(action, currentState){
 
 if(action.type === 'INCREASE') {
 return currentState + 1;
 }
 
 return currentState;
 }
const currentState = 0;
 const action = {type: 'INCREASE'};
 
 function reducer(action, currentState){
 
 if(action.type === 'INCREASE') {
 return currentState + 1;
 }
 
 return currentState;
 }
const currentState = 0;
 const action = {type: 'INCREASE'};
 
 function reducer(action, currentState){
 
 if(action.type === 'INCREASE') {
 return currentState + 1;
 }
 
 return currentState;
 }
const currentState = 0;
 const action = {type: 'INCREASE'};
 
 function reducer(action, currentState){
 
 if(action.type === 'INCREASE') {
 return currentState + 1;
 }
 
 return currentState;
 }
const currentState = 0;
 const action = {type: 'INCREASE'};
 
 function reducer(action, currentState){
 
 if(action.type === 'INCREASE') {
 return currentState + 1;
 }
 
 return currentState;
 }
HOW TO USE REDUX
REDUX IS A TINY LIBRARY which contains a function for creating a store, and 4 other helpers functions that we can use.
import { createStore } from 'redux';
 
 const store = createStore(reducer);
import { createStore } from 'redux';
 
 const store = createStore(reducer);
STORE API getState() dispatch(action) subscribe(listener) replaceReducer(reducer)
THE STORE IS SIMPLE TO UNDERSTAND we can implement a working store in less then 30 lines of code.
function createStore(reducer) {
 let state = null;
 const listeners = [];
 
 function getState() {
 return state;
 }
 
 function dispatch(action) {
 state = reducer(state, action);
 listeners.forEach(listener => listener())
 }
 
 function subscribe(listener) {
 listeners.push(listener);
 return function () {
 let index = listeners.indexOf(listener);
 listeners.splice(index, 1)
 }
 }
 
 dispatch({ type:’@@INIT' });
 return { getState, dispatch, subscribe }
 }
CONNECTING THE DOTS now we can dispatch actions and be notified when the state has changed.
dispatch getState STATE ACTION STOREUI
UI IS RENDERED SOMETHING HAPPENED ACTION DISPATCHED STATE CHANGED UI IS RENDERED SOMETHING HAPPENED ACTION DISPATCHED STATE CHANGED SOMETHING HAPPENED
UNI-DIRECTIONAL DATA FLOW sync flow
{ LIVE CODING } let’s build a traffic light and see redux in action!
INTERCEPTING THE ACTION FLOW this is done using middleware which can catch the action before they hit the store. async flow
STATE ACTION STOREUI Middlewae
{ LIVE CODING } implementing a simple logger as a middleware.
ANGULAR & REDUX
JUST THE VIEW api as actions logic in middlewares & reducers state inside a store
ANGULAR APP IS A TREE OF COMPONENTS each component is responsible for rendering a a pert of the state, or dispatching actions.
CONTAINER COMPONENT COMPONENT STORE [properties] (events) actions state
BINDING COMPONENTS TO THE STORE it’s straightforward but requires a lot of boilerplate inside the component class.
NPM TO THE RESCUE! FIND AN ADAPTOR I will use the ‘angular2-redux-bindings’ for this example
class TodoListComponent { 
 constructor(){
 this.unsubscribe = store.subscribe( () => this.renderList )
 }
 ngOnInit(){
 this.renderList()
 }
 
 ngOnDestroy(){
 this.unsubscribe()
 }
 
 addItem(item){
 store.dispatch({
 type: 'ADD_ITEM',
 payload: item
 })
 }
 
 renderList(){
 this.list = store.getState().list;
 }
 }
class TodoListComponent {
 
 @MapState('list')
 private list;
 
 @BindActions(addItem)
 private addItem;
 
 }
{ LIVE CODING } let’s build the view layer using Angular2 components
NEXT STEPS
PLAY WITH THE DEMO https://github.com/nirkaufman/angular-redux-demo
Angular & Redux Workshop https://leanpub.com/redux-book REDUX BOOK
Angular & Redux Workshop FURTHER READING REDUX http://redux.js.org/ https://egghead.io/series/getting-started-with-redux CQRS & EVENT SOURCING https://msdn.microsoft.com/en-us/library/dn568103.aspx https://msdn.microsoft.com/en-us/library/dn589792.aspx ANGULAR 2 angular-2-change-detection-explained.html
THANK YOU!
Read our blog: http://blog.500tech.com NIR KAUFMAN nir@500tech.com Head of Angular Development @ 500Tech

redux and angular - up and running