Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Connect.Tech 2017: Get Started with Redux
Search
Jeremy Fairbank
September 21, 2017
Programming
470
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Connect.Tech 2017: Get Started with Redux
Jeremy Fairbank
September 21, 2017
More Decks by Jeremy Fairbank
See All by Jeremy Fairbank
Connect.Tech 2020: Advanced Cypress Testing
jfairbank
1
250
CodeMash 2020: Solving the Boolean Identity Crisis
jfairbank
1
210
CodeMash 2020: Practical Functional Programming
jfairbank
1
360
Connect.Tech 2019: Practical Functional Programming
jfairbank
0
420
Connect.Tech 2019: Solving the Boolean Identity Crisis
jfairbank
0
240
Lambda Squared 2019: Solving the Boolean Identity Crisis
jfairbank
0
180
All Things Open 2018: Practical Functional Programming
jfairbank
2
280
Connect.Tech 2018: Effective React Testing
jfairbank
1
210
Fluent Conf 2018: Building web apps with Elm Tutorial
jfairbank
2
930
Other Decks in Programming
See All in Programming
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
400
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
6.6k
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
760
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
2k
Oxlintのカスタムルールの現況
syumai
6
1.1k
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
6
1.1k
net-httpのHTTP/2対応について
naruse
0
480
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
dRuby over BLE
makicamel
2
340
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
630
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
430
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Ruling the World: When Life Gets Gamed
codingconduct
0
250
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
310
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
The Curse of the Amulet
leimatthew05
1
13k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
840
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
250
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
590
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Transcript
Jeremy Fairbank @elpapapollo / jfairbank Get Started with Redux
Software is broken. We are here to fix it. Say
[email protected]
The Wild West of State
<div data-id="42" data-name="Tucker"> ... </div> var $el = $('[data-id="42"]'); var
currentName = $el.data('name'); $el.data('name', currentName.toUpperCase()); Data in the DOM
Controller View Model Model Model Model Model View View View
View MVC
View Model Model Model Model Model View Model View Model
View Model View Model Model Two-way Data Binding
The Wild West of State State is everywhere and anyone
can change it!
Predictable State Container
All application state in one place State Container
Predictable Old State REDUCER New State
Predictable Old State REDUCER New State • State changes in
one place
Predictable Old State REDUCER New State • State changes in
one place • State changes in well-defined ways
Predictable Old State REDUCER New State • State changes in
one place • State changes in well-defined ways • Changes are serialized
Reducer View State Actions
Reducer View State Actions
Reducer View React, Angular, Vanilla JS, etc. State Actions
Reducer View State Actions Dispatch
Reducer View State Actions
<button id="decrement">-</button> <div id="counter">0</div> <button id="increment">+</button>
incrementBtn.addEventListener('click', () => { counter.innerText = Number(counter.innerText) + 1; });
decrementBtn.addEventListener('click', () => { counter.innerText = Number(counter.innerText) - 1; });
let state = 0; function render() { counter.innerText = state;
} incrementBtn.addEventListener('click', () => { state += 1; render(); }); decrementBtn.addEventListener('click', () => { state -= 1; render(); });
let state = 0; function render() { counter.innerText = state;
} incrementBtn.addEventListener('click', () => { state += 1; render(); }); decrementBtn.addEventListener('click', () => { state -= 1; render(); });
let state = 0; function render() { counter.innerText = state;
} incrementBtn.addEventListener('click', () => { state += 1; render(); }); decrementBtn.addEventListener('click', () => { state -= 1; render(); });
let state = 0; function render() { counter.innerText = state;
} incrementBtn.addEventListener('click', () => { state += 1; render(); }); decrementBtn.addEventListener('click', () => { state -= 1; render(); });
state += 1; state -= 1; Anyone can access and
mutate state
state += 1 state -= 1 { type: 'INCREMENT' }
{ type: 'DECREMENT' } Tokens/descriptors that describe a type of change. Actions
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
function reducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } } Returns new state from current state and action. Reducer
Updating state is just a function call now let state
= 0; state = reducer(state, { type: 'INCREMENT' }); // 1 state = reducer(state, { type: 'INCREMENT' }); // 2 state = reducer(state, { type: 'DECREMENT' }); // 1 state = reducer(state, { type: 'ADD_2' }); // 1 console.log(state); // 1
Updating state is just a function call now let state
= 0; state = reducer(state, { type: 'INCREMENT' }); // 1 state = reducer(state, { type: 'INCREMENT' }); // 2 state = reducer(state, { type: 'DECREMENT' }); // 1 state = reducer(state, { type: 'ADD_2' }); // 1 console.log(state); // 1 Unhandled types are ignored
function dispatch(action) { state = reducer(state, action); render(); } incrementBtn.addEventListener('click',
() => { dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click', () => { dispatch({ type: 'DECREMENT' }); }); Decouple event from state change.
function dispatch(action) { state = reducer(state, action); render(); } incrementBtn.addEventListener('click',
() => { dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click', () => { dispatch({ type: 'DECREMENT' }); }); Decouple event from state change.
function dispatch(action) { state = reducer(state, action); render(); } incrementBtn.addEventListener('click',
() => { dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click', () => { dispatch({ type: 'DECREMENT' }); }); Decouple event from state change.
function dispatch(action) { state = reducer(state, action); render(); } incrementBtn.addEventListener('click',
() => { dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click', () => { dispatch({ type: 'DECREMENT' }); }); Decouple event from state change.
function dispatch(action) { state = reducer(state, action); render(); } incrementBtn.addEventListener('click',
() => { dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click', () => { dispatch({ type: 'DECREMENT' }); }); Decouple event from state change.
STORE
import { createStore } from 'redux'; const store = createStore(reducer,
0); store.getState(); // 0
import { createStore } from 'redux'; const store = createStore(reducer,
0); store.getState(); // 0
import { createStore } from 'redux'; const store = createStore(reducer,
0); store.getState(); // 0
import { createStore } from 'redux'; const store = createStore(reducer,
0); store.getState(); // 0
import { createStore } from 'redux'; const store = createStore(reducer,
0); store.getState(); // 0
incrementBtn.addEventListener('click', () => { store.dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click',
() => { store.dispatch({ type: 'DECREMENT' }); }); store.subscribe(() => { counter.innerText = store.getState(); });
incrementBtn.addEventListener('click', () => { store.dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click',
() => { store.dispatch({ type: 'DECREMENT' }); }); store.subscribe(() => { counter.innerText = store.getState(); });
incrementBtn.addEventListener('click', () => { store.dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click',
() => { store.dispatch({ type: 'DECREMENT' }); }); store.subscribe(() => { counter.innerText = store.getState(); });
Store Reducer State View 0
Store Reducer State View 0
Store Reducer State View 0 getState
Store Reducer View State 0
Store Reducer View State 0 INCREMENT dispatch
Store Reducer View State 0 INCREMENT dispatch
Store Reducer View State 0
Store Reducer View State 1
Store Reducer View State 1
Store Reducer View State 1 1 subscribe getState
incrementBtn.addEventListener('click', () => { store.dispatch({ type: 'INCREMENT' }); }); decrementBtn.addEventListener('click',
() => { store.dispatch({ type: 'DECREMENT' }); }); Problems: • Creating actions are cumbersome • Requires direct access to store
const increment = () => ({ type: 'INCREMENT', }); const
decrement = () => ({ type: 'DECREMENT', }); Reusable functions that create actions Action Creators
const increment = () => ({ type: 'INCREMENT', }); const
decrement = () => ({ type: 'DECREMENT', }); Reusable functions that create actions Action Creators
const increment = () => ({ type: 'INCREMENT', }); const
decrement = () => ({ type: 'DECREMENT', }); Reusable functions that create actions Action Creators
incrementBtn.addEventListener('click', () => { store.dispatch(increment()); }); decrementBtn.addEventListener('click', () => {
store.dispatch(decrement()); }); Problems: • Creating actions are cumbersome • Requires direct access to store
const actions = { increment: () => store.dispatch(increment()), decrement: ()
=> store.dispatch(decrement()), }; Automatically dispatch when invoked Bound Action Creators
const actions = { increment: () => store.dispatch(increment()), decrement: ()
=> store.dispatch(decrement()), }; Automatically dispatch when invoked Bound Action Creators Manually created
const actions = { increment: () => store.dispatch(increment()), decrement: ()
=> store.dispatch(decrement()), }; Automatically dispatch when invoked Bound Action Creators
import { bindActionCreators } from 'redux'; const actions = bindActionCreators(
{ increment, decrement }, store.dispatch ); Automatically dispatch when invoked Bound Action Creators
import { bindActionCreators } from 'redux'; const actions = bindActionCreators(
{ increment, decrement }, store.dispatch ); Automatically dispatch when invoked Bound Action Creators
import { bindActionCreators } from 'redux'; const actions = bindActionCreators(
{ increment, decrement }, store.dispatch ); Automatically dispatch when invoked Bound Action Creators Automatically created
Problems: • Creating actions are cumbersome • Requires direct access
to store incrementBtn.addEventListener('click', actions.increment); decrementBtn.addEventListener('click', actions.decrement);
React +
github.com/reactjs/react-redux npm install --save react-redux Official React bindings for Redux
React Redux Library
Reducer State Actions React Redux
React Redux React Application
React Redux React Application Provider
Component React Redux React Application Provider connect State Action Creators
Component Child React Redux React Application Provider connect State Child
Action Creators
const MyApp = () => ( <div> <button>-</button> <div>0</div> <button>+</button>
</div> );
import React from 'react'; import { render } from 'react-dom';
import { Provider } from 'react-redux'; render(( <Provider store={store}> <MyAppContainer /> </Provider> ), document.getElementById('main'));
import React from 'react'; import { render } from 'react-dom';
import { Provider } from 'react-redux'; render(( <Provider store={store}> <MyAppContainer /> </Provider> ), document.getElementById('main'));
import React from 'react'; import { render } from 'react-dom';
import { Provider } from 'react-redux'; render(( <Provider store={store}> <MyAppContainer /> </Provider> ), document.getElementById('main'));
import { connect } from 'react-redux'; const mapStateToProps = counter
=> ({ counter }); function mapDispatchToProps(dispatch) { return bindActionCreators({ onIncrement: increment, onDecrement: decrement, }, dispatch); } const MyAppContainer = connect( mapStateToProps, mapDispatchToProps )(MyApp);
import { connect } from 'react-redux'; const mapStateToProps = counter
=> ({ counter }); function mapDispatchToProps(dispatch) { return bindActionCreators({ onIncrement: increment, onDecrement: decrement, }, dispatch); } const MyAppContainer = connect( mapStateToProps, mapDispatchToProps )(MyApp);
import { connect } from 'react-redux'; const mapStateToProps = counter
=> ({ counter }); function mapDispatchToProps(dispatch) { return bindActionCreators({ onIncrement: increment, onDecrement: decrement, }, dispatch); } const MyAppContainer = connect( mapStateToProps, mapDispatchToProps )(MyApp);
import { connect } from 'react-redux'; const mapStateToProps = counter
=> ({ counter }); function mapDispatchToProps(dispatch) { return bindActionCreators({ onIncrement: increment, onDecrement: decrement, }, dispatch); } const MyAppContainer = connect( mapStateToProps, mapDispatchToProps )(MyApp);
import { connect } from 'react-redux'; const mapStateToProps = counter
=> ({ counter }); function mapDispatchToProps(dispatch) { return bindActionCreators({ onIncrement: increment, onDecrement: decrement, }, dispatch); } const MyAppContainer = connect( mapStateToProps, mapDispatchToProps )(MyApp);
const MyApp = (props) => ( <div> <button onClick={props.onDecrement}> -
</button> <div>{props.counter}</div> <button onClick={props.onIncrement}> + </button> </div> );
const MyApp = (props) => ( <div> <button onClick={props.onDecrement}> -
</button> <div>{props.counter}</div> <button onClick={props.onIncrement}> + </button> </div> ); Store state mapStateToProps
const MyApp = (props) => ( <div> <button onClick={props.onDecrement}> -
</button> <div>{props.counter}</div> <button onClick={props.onIncrement}> + </button> </div> ); Bound action creators mapDispatchToProps
Immutable Object State
const initialState = { counter: 0, car: { color: 'red',
}, };
const initialState = { counter: 0, car: { color: 'red',
}, };
const initialState = { counter: 0, car: { color: 'red',
}, };
const increment = () => ({ type: 'INCREMENT', }); const
decrement = () => ({ type: 'DECREMENT', }); const changeColor = color => ({ type: 'CHANGE_COLOR', payload: color, });
const increment = () => ({ type: 'INCREMENT', }); const
decrement = () => ({ type: 'DECREMENT', }); const changeColor = color => ({ type: 'CHANGE_COLOR', payload: color, });
const increment = () => ({ type: 'INCREMENT', }); const
decrement = () => ({ type: 'DECREMENT', }); const changeColor = color => ({ type: 'CHANGE_COLOR', payload: color, });
function reducer(state = initialState, action) { switch (action.type) { case
'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'CHANGE_COLOR': return { ...state, car: { color: action.payload } }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'CHANGE_COLOR': return { ...state, car: { color: action.payload } }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'CHANGE_COLOR': return { ...state, car: { color: action.payload } }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'CHANGE_COLOR': return { ...state, car: { color: action.payload } }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'CHANGE_COLOR': return { ...state, car: { color: action.payload } }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'INCREMENT': return { ...state, counter: state.counter + 1 }; case 'DECREMENT': return { ...state, counter: state.counter - 1 }; case 'CHANGE_COLOR': return { ...state, car: { color: action.payload } }; default: return state; } }
store.subscribe(() => { console.log('state =', store.getState()); }); store.dispatch(increment()); store.dispatch(changeColor('green')); //
state = { counter: 1, car: { color: 'red' } } // state = { counter: 1, car: { color: 'green' } }
store.subscribe(() => { console.log('state =', store.getState()); }); store.dispatch(increment()); store.dispatch(changeColor('green')); //
state = { counter: 1, car: { color: 'red' } } // state = { counter: 1, car: { color: 'green' } }
store.subscribe(() => { console.log('state =', store.getState()); }); store.dispatch(increment()); store.dispatch(changeColor('green')); //
state = { counter: 1, car: { color: 'red' } } // state = { counter: 1, car: { color: 'green' } }
function reducer(state = initialState, action) { }
Reducer Composition Create modular reducers for better organization and readability
Root Reducer Counter Reducer Car Reducer
function counterReducer(state = 0, action) { switch (action.type) { case
'INCREMENT': return state + 1; case 'DECREMENT': return state - 1; default: return state; } }
const initialState = { color: 'red' }; function carReducer(state =
initialState, action) { switch (action.type) { case 'CHANGE_COLOR': return { ...state, color: action.payload }; default: return state; } }
const initialState = { color: 'red' }; function carReducer(state =
initialState, action) { switch (action.type) { case 'CHANGE_COLOR': return { ...state, color: action.payload }; default: return state; } }
const initialState = { color: 'red' }; function carReducer(state =
initialState, action) { switch (action.type) { case 'CHANGE_COLOR': return { ...state, color: action.payload }; default: return state; } }
function reducer(state = {}, action) { return { counter: counterReducer(state.counter,
action), car: carReducer(state.car, action), }; }
function reducer(state = {}, action) { return { counter: counterReducer(state.counter,
action), car: carReducer(state.car, action), }; }
function reducer(state = {}, action) { return { counter: counterReducer(state.counter,
action), car: carReducer(state.car, action), }; }
function reducer(state = {}, action) { return { counter: counterReducer(state.counter,
action), car: carReducer(state.car, action), }; }
import { combineReducers } from 'redux'; const reducer = combineReducers({
counter: counterReducer, car: carReducer, });
import { combineReducers } from 'redux'; const reducer = combineReducers({
counter: counterReducer, car: carReducer, });
import { combineReducers } from 'redux'; const reducer = combineReducers({
counter: counterReducer, car: carReducer, });
Root Reducer Counter Reducer Car Reducer Engine Reducer Tire Reducer
… … …
Root Reducer Counter Reducer Car Reducer Engine Reducer Tire Reducer
… … …
Root Reducer Counter Reducer Car Reducer Engine Reducer Tire Reducer
… … …
Interact with APIs?
Middleware Enhance Redux applications
• Logging Middleware Enhance Redux applications
• Logging • Debugging Middleware Enhance Redux applications
• Logging • Debugging • API interaction Middleware Enhance Redux
applications
• Logging • Debugging • API interaction • Custom actions
Middleware Enhance Redux applications
Reducer View State Actions Middleware
Reducer View State Actions Middleware Intercept
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
const logMiddleware = api => next => action => {
console.log('dispatch', action); const result = next(action); console.log('state =', api.getState()); return result; };
import { applyMiddleware } from 'redux'; const store = createStore(
reducer, applyMiddleware(logMiddleware) ); store.dispatch(increment()); store.dispatch(changeColor('green')); // dispatch { type: 'INCREMENT' } // state = { counter: 1, car: { color: 'red' } } // dispatch { type: 'CHANGE_COLOR', payload: 'green' } // state = { counter: 1, car: { color: 'green' } }
import { applyMiddleware } from 'redux'; const store = createStore(
reducer, applyMiddleware(logMiddleware) ); store.dispatch(increment()); store.dispatch(changeColor('green')); // dispatch { type: 'INCREMENT' } // state = { counter: 1, car: { color: 'red' } } // dispatch { type: 'CHANGE_COLOR', payload: 'green' } // state = { counter: 1, car: { color: 'green' } }
import { applyMiddleware } from 'redux'; const store = createStore(
reducer, applyMiddleware(logMiddleware) ); store.dispatch(increment()); store.dispatch(changeColor('green')); // dispatch { type: 'INCREMENT' } // state = { counter: 1, car: { color: 'red' } } // dispatch { type: 'CHANGE_COLOR', payload: 'green' } // state = { counter: 1, car: { color: 'green' } }
import { applyMiddleware } from 'redux'; const store = createStore(
reducer, applyMiddleware(logMiddleware) ); store.dispatch(increment()); store.dispatch(changeColor('green')); // dispatch { type: 'INCREMENT' } // state = { counter: 1, car: { color: 'red' } } // dispatch { type: 'CHANGE_COLOR', payload: 'green' } // state = { counter: 1, car: { color: 'green' } }
import { applyMiddleware } from 'redux'; const store = createStore(
reducer, applyMiddleware(logMiddleware) ); store.dispatch(increment()); store.dispatch(changeColor('green')); // dispatch { type: 'INCREMENT' } // state = { counter: 1, car: { color: 'red' } } // dispatch { type: 'CHANGE_COLOR', payload: 'green' } // state = { counter: 1, car: { color: 'green' } }
const initialState = { status: 'READY', user: null, };
function reducer(state = initialState, action) { switch (action.type) { case
'REQUEST_USER': return { ...state, status: 'FETCHING' }; case 'RECEIVE_USER': return { ...state, status: 'SUCCESS', user: action.payload, }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'REQUEST_USER': return { ...state, status: 'FETCHING' }; case 'RECEIVE_USER': return { ...state, status: 'SUCCESS', user: action.payload, }; default: return state; } }
function reducer(state = initialState, action) { switch (action.type) { case
'REQUEST_USER': return { ...state, status: 'FETCHING' }; case 'RECEIVE_USER': return { ...state, status: 'SUCCESS', user: action.payload, }; default: return state; } }
const requestUser = id => ({ type: 'REQUEST_USER', payload: id,
}); const receiveUser = user => ({ type: 'RECEIVE_USER', payload: user, });
const store = createStore(reducer); function fetchUser(id) { store.dispatch(requestUser(id)); axios.get(`/users/${id}`) .then(({
data: user }) => { store.dispatch(receiveUser(user)); }); } fetchUser(1);
const store = createStore(reducer); function fetchUser(id) { store.dispatch(requestUser(id)); axios.get(`/users/${id}`) .then(({
data: user }) => { store.dispatch(receiveUser(user)); }); } fetchUser(1);
const store = createStore(reducer); function fetchUser(id) { store.dispatch(requestUser(id)); axios.get(`/users/${id}`) .then(({
data: user }) => { store.dispatch(receiveUser(user)); }); } fetchUser(1);
const thunkMiddleware = api => next => action => {
if (typeof action === 'function') { return action(api.dispatch); } return next(action); }; const store = createStore( reducer, applyMiddleware(thunkMiddleware) );
const thunkMiddleware = api => next => action => {
if (typeof action === 'function') { return action(api.dispatch); } return next(action); }; const store = createStore( reducer, applyMiddleware(thunkMiddleware) );
const thunkMiddleware = api => next => action => {
if (typeof action === 'function') { return action(api.dispatch); } return next(action); }; const store = createStore( reducer, applyMiddleware(thunkMiddleware) );
const thunkMiddleware = api => next => action => {
if (typeof action === 'function') { return action(api.dispatch); } return next(action); }; const store = createStore( reducer, applyMiddleware(thunkMiddleware) );
function fetchUser(id) { return dispatch => { dispatch(requestUser(id)); return axios.get(`/users/${id}`)
.then(({ data: user }) => { dispatch(receiveUser(user)); }); }; } store.dispatch(fetchUser(1));
function fetchUser(id) { return dispatch => { dispatch(requestUser(id)); return axios.get(`/users/${id}`)
.then(({ data: user }) => { dispatch(receiveUser(user)); }); }; } store.dispatch(fetchUser(1)); Action Creator
function fetchUser(id) { return dispatch => { dispatch(requestUser(id)); return axios.get(`/users/${id}`)
.then(({ data: user }) => { dispatch(receiveUser(user)); }); }; } store.dispatch(fetchUser(1)); Action
function fetchUser(id) { return dispatch => { dispatch(requestUser(id)); return axios.get(`/users/${id}`)
.then(({ data: user }) => { dispatch(receiveUser(user)); }); }; } store.dispatch(fetchUser(1));
Alternative Async Middleware
redux-saga.js.org
• Uses ES6 generator functions redux-saga.js.org
• Uses ES6 generator functions • Write asynchronous code in
synchronous manner redux-saga.js.org
• Uses ES6 generator functions • Write asynchronous code in
synchronous manner • Great for coordinating multiple API calls redux-saga.js.org
• Uses ES6 generator functions • Write asynchronous code in
synchronous manner • Great for coordinating multiple API calls • Great for forking background tasks redux-saga.js.org
redux-observable.js.org
redux-observable.js.org • Uses RxJS observables
redux-observable.js.org • Uses RxJS observables • Write asynchronous code with
declarative observable chains
redux-observable.js.org • Uses RxJS observables • Write asynchronous code with
declarative observable chains • Great for composing asynchronous operations
redux-observable.js.org • Uses RxJS observables • Write asynchronous code with
declarative observable chains • Great for composing asynchronous operations • Very concise code
And plenty more options…
• redux-logic And plenty more options…
• redux-logic • redux-ship And plenty more options…
• redux-logic • redux-ship • redux-promise And plenty more options…
• redux-logic • redux-ship • redux-promise • redux-api-middleware And plenty
more options…
× ✓ Testing
const state = { counter: 0, car: { color: 'red'
}, }; it('returns initial state', () => { expect(reducer(undefined, {})).toEqual(state); }); it('increments the number', () => { const subject = reducer(state, increment()).counter; expect(subject).toBe(1); }); it('changes the car color', () => { const subject = reducer(state, changeColor('green')).car.color; expect(subject).toBe('green'); }); Easy reducer unit tests!
it('creates an INCREMENT action', () => { expect(increment()).toEqual({ type: 'INCREMENT'
}); }); it('creates a CHANGE_COLOR action', () => { expect(changeColor('blue')).toEqual({ type: 'CHANGE_COLOR', payload: 'blue', }); }); You can test action creators, but not really necessary.
However, you should test async action creators. function fetchUser(id) {
return dispatch => { dispatch(requestUser(id)); return axios.get(`/users/${id}`) .then(({ data: user }) => { dispatch(receiveUser(user)); }); }; }
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); }); Unit test with test doubles
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
import td from 'testdouble'; const axios = td.replace('axios'); it('fetches a
user', () => { // Arrange const dispatch = td.function(); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act return fetchUser(1)(dispatch).then(() => { const subject = td.matchers.captor(); td.verify(dispatch(subject.capture())); // Assert expect(subject.values[0]).toEqual(requestUser(1)); expect(subject.values[1]).toEqual(receiveUser('fake user’)); }); });
Use integration tests to ensure all pieces work together. Allow
store, reducer, and actions to all interact .
it('fetches a user', () => { // Arrange const store
= createStore(reducer, applyMiddleware(thunkMiddleware)); td.replace(axios, 'get'); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act const promise = store.dispatch(fetchUser(1)); const subject = store.getState; // Assert expect(subject()).toEqual({ status: 'FETCHING', user: null }); return promise.then(() => { expect(subject()).toEqual({ status: 'SUCCESS', user: 'fake user' }); }); });
it('fetches a user', () => { // Arrange const store
= createStore(reducer, applyMiddleware(thunkMiddleware)); td.replace(axios, 'get'); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act const promise = store.dispatch(fetchUser(1)); const subject = store.getState; // Assert expect(subject()).toEqual({ status: 'FETCHING', user: null }); return promise.then(() => { expect(subject()).toEqual({ status: 'SUCCESS', user: 'fake user' }); }); });
it('fetches a user', () => { // Arrange const store
= createStore(reducer, applyMiddleware(thunkMiddleware)); td.replace(axios, 'get'); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act const promise = store.dispatch(fetchUser(1)); const subject = store.getState; // Assert expect(subject()).toEqual({ status: 'FETCHING', user: null }); return promise.then(() => { expect(subject()).toEqual({ status: 'SUCCESS', user: 'fake user' }); }); });
it('fetches a user', () => { // Arrange const store
= createStore(reducer, applyMiddleware(thunkMiddleware)); td.replace(axios, 'get'); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act const promise = store.dispatch(fetchUser(1)); const subject = store.getState; // Assert expect(subject()).toEqual({ status: 'FETCHING', user: null }); return promise.then(() => { expect(subject()).toEqual({ status: 'SUCCESS', user: 'fake user' }); }); });
it('fetches a user', () => { // Arrange const store
= createStore(reducer, applyMiddleware(thunkMiddleware)); td.replace(axios, 'get'); td.when(axios.get('/users/1')).thenResolve({ data: 'fake user' }); // Act const promise = store.dispatch(fetchUser(1)); const subject = store.getState; // Assert expect(subject()).toEqual({ status: 'FETCHING', user: null }); return promise.then(() => { expect(subject()).toEqual({ status: 'SUCCESS', user: 'fake user' }); }); });
Resources • Redux • redux.js.org • egghead.io/courses/getting-started-with- redux • React
• github.com/reactjs/react-redux
Thanks! Slides: bit.ly/redux-connect Jeremy Fairbank @elpapapollo / jfairbank