82
Designing applications with Redux

Designing applications with Redux

Embed Size (px)

Citation preview

Page 1: Designing applications with Redux

Designing applicationswith Redux

Page 2: Designing applications with Redux

Fernando Daciuknpm install fdaciuk

Page 3: Designing applications with Redux

http://zimp.me

Page 4: Designing applications with Redux

da2k.com.br

Page 5: Designing applications with Redux

Daciuk = Da”Two”k

Page 6: Designing applications with Redux

Daciuk = Da2k

Page 7: Designing applications with Redux
Page 8: Designing applications with Redux
Page 9: Designing applications with Redux

JAVASCRIPTNINJA

eventick.com.br/curso-javascript-ninja

Page 10: Designing applications with Redux
Page 11: Designing applications with Redux
Page 12: Designing applications with Redux

“Redux is a predictable state container

for JavaScript apps”http://redux.js.org

Page 13: Designing applications with Redux

Why have Reduxwas created?

Page 14: Designing applications with Redux

“Once upona time…”

Page 15: Designing applications with Redux

Serverresponses

Page 16: Designing applications with Redux

Cacheddata

Page 17: Designing applications with Redux

Local non-persistent data

Page 18: Designing applications with Redux

UIstate

Page 19: Designing applications with Redux

Managing this ever-change state

is hard!

Page 20: Designing applications with Redux
Page 21: Designing applications with Redux

Mutation andasynchronicity

Page 22: Designing applications with Redux

Mentos &Coke

Page 23: Designing applications with Redux

React removes async and directDOM manipulation

Page 24: Designing applications with Redux
Page 25: Designing applications with Redux

You even needmanage state

Page 26: Designing applications with Redux

Behold, theFlux comes!

Page 27: Designing applications with Redux

Redux makes predictablestate changes

Page 28: Designing applications with Redux

Redux:three principles

Page 29: Designing applications with Redux

Redux:three principles

1st. Single sourceof truth

Page 30: Designing applications with Redux

store

Redux:three principles

Page 31: Designing applications with Redux

store

Redux:three principles

{ counter: 0, todos: [{ … }]}

Page 32: Designing applications with Redux

Redux:three principles

2nd. state isread-only

Page 33: Designing applications with Redux

actions

Redux:three principles

Page 34: Designing applications with Redux

actions{ type: ‘WHAT_TODO’ }

Redux:three principles

Page 35: Designing applications with Redux

Redux:three principles

3rd. changes are madewith pure functions

Page 36: Designing applications with Redux

reducers

Redux:three principles

Page 37: Designing applications with Redux

reducersfunction reducer (state, action) => state

Redux:three principles

Page 38: Designing applications with Redux

Whyreducer?

Page 39: Designing applications with Redux

Whyreducer?

var arr = [1, 2, 3];

var sum = arr.reduce(function(acc, item) {

return acc + item;

}, 0);

console.log(sum); // 6

Page 40: Designing applications with Redux

Whyreducer?

var arr = [1, 2, 3];

var sum = arr.reduce(function(acc, item) {

return acc + item;

}, 0);

console.log(sum); // 6

reducer

Page 41: Designing applications with Redux

Changing toES2015 (ES6)

Page 42: Designing applications with Redux

var arr = [1, 2, 3];

var sum = arr.reduce(function(acc, item) {

return acc + item;

}, 0);

console.log(sum); // 6

Whyreducer?

Page 43: Designing applications with Redux

const arr = [1, 2, 3];

var sum = arr.reduce(function(acc, item) {

return acc + item;

}, 0);

console.log(sum); // 6

Whyreducer?

Page 44: Designing applications with Redux

const arr = [1, 2, 3];

const sum = arr.reduce(function(acc, item) {

return acc + item;

}, 0);

console.log(sum); // 6

Whyreducer?

Page 45: Designing applications with Redux

const arr = [1, 2, 3];

const sum = arr.reduce((acc, item) => {

return acc + item;

}, 0);

console.log(sum); // 6

Whyreducer?

Page 46: Designing applications with Redux

const arr = [1, 2, 3];

const sum = arr.reduce((acc, item) =>

acc + item

, 0);

console.log(sum); // 6

Whyreducer?

Page 47: Designing applications with Redux

const arr = [1, 2, 3];

const sum = arr.reduce((acc, item) => acc + item

, 0);

console.log(sum); // 6

Whyreducer?

Page 48: Designing applications with Redux

const arr = [1, 2, 3];

const sum = arr.reduce((acc, item) => acc + item, 0);

console.log(sum); // 6

Whyreducer?

Page 49: Designing applications with Redux

Anotherreducer example

Page 50: Designing applications with Redux

Anotherreducer example

const arr = [1, 2, 3];

const state = arr.reduce((acc, item) => {

return acc.concat(item + 1);

}, []);

console.log(state); // [2, 3, 4]

Page 51: Designing applications with Redux

Creating acounter

Page 52: Designing applications with Redux

RULES

Creating acounter

Page 53: Designing applications with Redux

action ‘INCREMENT’ => plus 1

Creating acounter

Page 54: Designing applications with Redux

action ‘INCREMENT’ => plus 1

action ‘DECREMENT’ => minus 1

Creating acounter

Page 55: Designing applications with Redux

action ‘INCREMENT’ => plus 1

action ‘DECREMENT’ => minus 1

action ‘UNKNOWN’ => previous state

Creating acounter

Page 56: Designing applications with Redux

action ‘INCREMENT’ => plus 1

action ‘DECREMENT’ => minus 1

action ‘UNKNOWN’ => previous state

state undefined => initial state (0)

Creating acounter

Page 57: Designing applications with Redux

1st step:adding redux

Creating acounter

Page 58: Designing applications with Redux

npm i --save redux

Creating acounter

~5.9kb - minified~2kb - gzipped

Page 59: Designing applications with Redux

https://cdnjs.cloudflare.com/ajax/libs/redux/3.3.1/redux.js

Creating acounter

Page 60: Designing applications with Redux

Redux methodcreateStore

Page 61: Designing applications with Redux

createStore(reducer)

Page 62: Designing applications with Redux

2nd step:creating the reducer

Creating acounter

Page 63: Designing applications with Redux

Creating acounter

counter.js

const counter = (state, action) => {

return state;

}

Page 64: Designing applications with Redux

Creating acounter

counter.js

const counter = (state, action) => {

switch(action.type) {

case 'INCREMENT': return state + 1;

}

}

Page 65: Designing applications with Redux

Creating acounter

counter.js

const counter = (state, action) => {

switch(action.type) {

case 'INCREMENT': return state + 1;

case 'DECREMENT': return state - 1;

}

}

Page 66: Designing applications with Redux

Creating acounter

counter.js

const counter = (state, action) => {

switch(action.type) {

case 'INCREMENT': return state + 1;

case 'DECREMENT': return state - 1;

}

return state;

}

Page 67: Designing applications with Redux

Creating acounter

counter.js

const counter = (state = 0, action) => {

switch(action.type) {

case 'INCREMENT': return state + 1;

case 'DECREMENT': return state - 1;

}

return state;

}

Page 68: Designing applications with Redux

How can I usethis reducer?

Page 69: Designing applications with Redux

storemethods

Page 70: Designing applications with Redux

getState()

storemethods

Page 71: Designing applications with Redux

storemethods

import { createStore } from 'redux';

import counter from './reducers/counter';

const store = createStore(counter);

console.log(store.getState()); // 0

Page 72: Designing applications with Redux

dispatch(action)

storemethods

Page 73: Designing applications with Redux

storemethods

import { createStore } from 'redux';

import counter from './reducers/counter';

const store = createStore(counter);

console.log(store.getState()); // 0

store.dispatch({ type: 'INCREMENT' });

console.log(store.getState()); // 1

Page 74: Designing applications with Redux

subscribe(listener)

storemethods

Page 75: Designing applications with Redux

storemethodsimport { createStore } from 'redux';

import counter from './reducers/counter';

const store = createStore(counter);

store.subscribe(() => {

console.log(store.getState());

});

store.dispatch({ type: 'INCREMENT' }); // 1

store.dispatch({ type: 'INCREMENT' }); // 2

store.dispatch({ type: 'DECREMENT' }); // 1

Page 76: Designing applications with Redux

We have timefor a full demo?

Page 77: Designing applications with Redux

Redux supportsmiddlewares

Page 78: Designing applications with Redux

async stateredux-thunk

gaearon/redux-thunk

Page 79: Designing applications with Redux

async stateredux-saga

yelouafi/redux-saga

Page 80: Designing applications with Redux

http://redux.js.org

Page 81: Designing applications with Redux

Think outsidethe box!Think outside

Page 82: Designing applications with Redux

Fernando DaciukFullStack Engineer

Thanks!

/fdaciuk/talks npm install fdaciuk