the hitchhiker’s guide to redux

32
The Hitchhiker’s Guide to Redux Sam Hogarth @samhogy

Upload: sam-hogarth

Post on 24-Jan-2018

379 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: The Hitchhiker’s Guide to Redux

The Hitchhiker’s Guide to Redux

Sam Hogarth@samhogy

Page 2: The Hitchhiker’s Guide to Redux

A predictable state container for

JavaScript applications

redux

Page 3: The Hitchhiker’s Guide to Redux

Web App

LiveStream

Saved Data

User

API

Logging

Page 4: The Hitchhiker’s Guide to Redux

facebook.github.io/flux/docs/overview

Non-deterministic systems:hard to debug

Page 5: The Hitchhiker’s Guide to Redux

Change Update Redraw

“Unidrectional Architecture”

UI Events

API Responses

Web Sockets

Page 6: The Hitchhiker’s Guide to Redux

1) A single source of truth for your application state - as an object tree

2) State is read-only, only actions can change the state

3) Updates to the state are made with pure functions

The Principles of Redux

Page 7: The Hitchhiker’s Guide to Redux

Store

1) A single source of truth

for your application state -

as an object tree

{authors: {author_1: {

name: “Sam Hogarth”}

},articles: {article_1: {

title: “Redux is AWSM”,content: “I’ll show ya”author: author_11

}}

}createStore(…)

Page 8: The Hitchhiker’s Guide to Redux

2) State is read-only, only

actions can change the state

getState()dispatch(action)

Action

{type: “SET_TITLE”payload: {article: 1,title: “Redux is Ace”

}}

Store

Page 9: The Hitchhiker’s Guide to Redux

Action Creator

2) State is read-only, only

actions can change the state

getState()dispatch(action)

function myAction (anArg) {

return {type: { … },payload: { … }

}}

Store

Page 10: The Hitchhiker’s Guide to Redux

3) Updates to the state are

made with pure functions

update (state, action) => newStatereduce

Reducer function

Page 11: The Hitchhiker’s Guide to Redux

pure functionA function whose output wholly

depends on its input arguments,

without producing side-effects

var arr = [2, 3, 4];

square(arr)> [4, 9, 16]

square(arr)> [4, 9, 16]> [16, 81, 256]

Math.random()> ?

Page 12: The Hitchhiker’s Guide to Redux

const initialState = { name: null };

const update = (state = initialState, action) => {

switch(action.type) {

case ‘SET_NAME’:return { …state, name: action.payload };

default:return state;

}};

export default update;

Immutable update

Page 13: The Hitchhiker’s Guide to Redux

Reducer

Reducer

Reducer

Reducer

Reducer

Reducer

Reducer

Reducer

root

combineReducers({subReducer1, subReducer2, subReducer3

})

Page 14: The Hitchhiker’s Guide to Redux

function compositionThe result of one function

becomes the input of the next.

The overall result is the result

of the last function.

const square = x => x * x;

cosnt double = x =>x + x;

const squareDbl = x =>square(double(x));

square(3)> 9

double(3)> 6

squareDbl(3)> 36

Page 15: The Hitchhiker’s Guide to Redux

3) Updates to the state are

made with pure functions

const store = createStore(rootReducer,initialState

);

Page 16: The Hitchhiker’s Guide to Redux

Render State in the UI

store.subscribe( () => {

const newState = store.getState();

render(newState);

});

Bindings exist for React & Angular2

Page 17: The Hitchhiker’s Guide to Redux

Change Update Redraw

{type,payload

}

Raise action Subscribe to store, get new state

Reducers

Redux as a Unidirectional Architecture

Page 18: The Hitchhiker’s Guide to Redux

Keeps reducers simple

Stops unexpected updates

{authors: {

author_1: {name: “Sam Hogarth”

}},articles: {

article_1: {title: “Redux is AWSM”,content: “I’ll show ya”author: “author_1”

},…

},publishedArticles: [

“article_1”]

}

normalization

Page 19: The Hitchhiker’s Guide to Redux

Q: How can we handle side-effects?

(Network Requests, Impure Functions)

Page 20: The Hitchhiker’s Guide to Redux

Middleware

A chain of functions that are called before the action reaches the reducers.

Use cases: Logging, Asynchronous Actions, Crash Reporting

Another Middleware

MiddlewareAction

Page 21: The Hitchhiker’s Guide to Redux

const middleware = applyMiddleware(middleware1,middleware2

);Order is important!

const store = createStore(rootReducer,initialState,middleware

);

Page 22: The Hitchhiker’s Guide to Redux

Q: How can we handle side-effects?

(Network Requests, Impure Functions)

A: Dispatch something to represent the

side-effect, use middleware to perform the side-effect

Page 23: The Hitchhiker’s Guide to Redux

thunkA function that wraps an

expression to delay its

execution

Page 24: The Hitchhiker’s Guide to Redux

function myAsynchronousAction(item) {

return (dispatch, getState) => {

dispatch({type: ‘ITEM_LOADING’

});

return fetch(`/api/items/${item}`).then(result => {

dispatch({type: ‘ITEM_RECEIVED’,payload: result

});

});}

Page 25: The Hitchhiker’s Guide to Redux

ThunkMiddleware

Action

ActionCreator

type !== function

Page 26: The Hitchhiker’s Guide to Redux

ThunkMiddleware

Thunk

ActionCreator

type === function

API

LoadingAction

ResolvedAction

Page 27: The Hitchhiker’s Guide to Redux

Developer Tooling

{type: { … },payload: { … }

}

{type: { … },payload: { … }

}

{type: { … },payload: { … }

}

{type: { … },payload: { … }

}

{type: { … },payload: { … }

}

Can be hot-swapped without affecting stored stateAn audit trail of

state changes

replaceReducer(r)

Page 28: The Hitchhiker’s Guide to Redux

Conclusion

Page 29: The Hitchhiker’s Guide to Redux

•Redux is mature, stable, and pretty much done

• Innovations are coming from helper libraries

redux-saga

reselect

Page 30: The Hitchhiker’s Guide to Redux

The concepts behind Redux will be useful, even if the library falls out of fashion

TIME-Travel Debugging

Immutable architecture

Serializable state

Pure update functions

Page 31: The Hitchhiker’s Guide to Redux

Alternatives

• Elm – a FP language for the web that inspired Redux

•MobX & CycleJS – Reactive-based state management

•Relay/GraphQL – Query & modify state like a database

Page 32: The Hitchhiker’s Guide to Redux

Questions?

Sam [email protected]