43
REDUX & ANGULAR UP & RUNNING NIR KAUFMAN

redux and angular - up and running

Embed Size (px)

Citation preview

Page 1: redux and angular - up and running

REDUX & ANGULAR UP & RUNNING

NIR KAUFMAN

Page 2: redux and angular - up and running

AGENDAwhat is redux? how to use redux? where Angular fits in? where to go from here?

Page 3: redux and angular - up and running

WHAT IS REDUX

Page 4: redux and angular - up and running

REDUX IS A DESIGN PATTERNfor managing a complex, ever changing state in a challenging modern single page applications

Page 5: redux and angular - up and running

SINGLE SOURCE OF TRUTHthe state of your whole application is stored within a single store

Page 6: redux and angular - up and running

THE STATE IS READ ONLYthe only way to mutate the state is to emit an action describing what happened

Page 7: redux and angular - up and running

PURE FUNCTIONSto specify how the state is transformed by actions, you write a pure function.

Page 8: redux and angular - up and running

PURE* FUNCTION

ACTION NEXT STATE

* No side-effects

reducer

Page 9: redux and angular - up and running

const currentState = 0; const action = {type: 'INCREASE'}; function reducer(action, currentState){ if(action.type === 'INCREASE') { return currentState + 1; } return currentState; }

Page 10: redux and angular - up and running

const currentState = 0; const action = {type: 'INCREASE'}; function reducer(action, currentState){ if(action.type === 'INCREASE') { return currentState + 1; } return currentState; }

Page 11: redux and angular - up and running

const currentState = 0; const action = {type: 'INCREASE'}; function reducer(action, currentState){ if(action.type === 'INCREASE') { return currentState + 1; } return currentState; }

Page 12: redux and angular - up and running

const currentState = 0; const action = {type: 'INCREASE'}; function reducer(action, currentState){ if(action.type === 'INCREASE') { return currentState + 1; } return currentState; }

Page 13: redux and angular - up and running

const currentState = 0; const action = {type: 'INCREASE'}; function reducer(action, currentState){ if(action.type === 'INCREASE') { return currentState + 1; } return currentState; }

Page 14: redux and angular - up and running

HOW TO USE REDUX

Page 15: redux and angular - up and running

REDUX IS A TINY LIBRARYwhich contains a function for creating a store, and 4 other helpers functions that we can use.

Page 16: redux and angular - up and running

import { createStore } from 'redux'; const store = createStore(reducer);

Page 17: redux and angular - up and running

import { createStore } from 'redux';const store = createStore(reducer);

Page 18: redux and angular - up and running

STORE APIgetState() dispatch(action)subscribe(listener)replaceReducer(reducer)

Page 19: redux and angular - up and running

THE STORE IS SIMPLE TO UNDERSTANDwe can implement a working store in less then 30 lines of code.

Page 20: redux and angular - up and running

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 } }

Page 21: redux and angular - up and running

CONNECTING THE DOTSnow we can dispatch actions and be notified when the state has changed.

Page 22: redux and angular - up and running

dispatch

getStateSTATE

ACTION

STOREUI

Page 23: redux and angular - up and running

UI IS RENDEREDSOMETHING HAPPENEDACTION DISPATCHEDSTATE CHANGEDUI IS RENDEREDSOMETHING HAPPENEDACTION DISPATCHEDSTATE CHANGEDSOMETHING HAPPENED

Page 24: redux and angular - up and running

UNI-DIRECTIONAL DATA FLOW

sync flow

Page 25: redux and angular - up and running

{ LIVE CODING }let’s build a traffic light and see redux in action!

Page 26: redux and angular - up and running

INTERCEPTING THE ACTION FLOWthis is done using middleware which can catch the action before they hit the store.

async flow

Page 27: redux and angular - up and running

STATE

ACTION

STOREUI

Middlewae

Page 28: redux and angular - up and running

{ LIVE CODING }implementing a simple logger as a middleware.

Page 29: redux and angular - up and running

ANGULAR & REDUX

Page 30: redux and angular - up and running

JUST THE VIEWapi as actions logic in middlewares & reducers state inside a store

Page 31: redux and angular - up and running

ANGULAR APP IS A TREE OF COMPONENTSeach component is responsible for rendering a a pert of the state, or dispatching actions.

Page 32: redux and angular - up and running

CONTAINER

COMPONENT COMPONENT

STORE

[properties] (events)

actions

state

Page 33: redux and angular - up and running

BINDING COMPONENTS TO THE STOREit’s straightforward but requires a lot of boilerplate inside the component class.

Page 34: redux and angular - up and running

NPM TO THE RESCUE! FIND AN ADAPTORI will use the ‘angular2-redux-bindings’ for this example

Page 35: redux and angular - up and running

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; } }

Page 36: redux and angular - up and running

class TodoListComponent { @MapState('list') private list; @BindActions(addItem) private addItem; }

Page 37: redux and angular - up and running

{ LIVE CODING }let’s build the view layer using Angular2 components

Page 38: redux and angular - up and running

NEXT STEPS

Page 39: redux and angular - up and running

PLAY WITH THE DEMO

https://github.com/nirkaufman/angular-redux-demo

Page 40: redux and angular - up and running

Angular & Redux Workshop

https://leanpub.com/redux-book

REDUX BOOK

Page 41: redux and angular - up and running

Angular & Redux Workshop

FURTHER READINGREDUX 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

Page 42: redux and angular - up and running

THANK YOU!

Page 43: redux and angular - up and running

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

NIR [email protected]

Head of Angular Development @ 500Tech