SlideShare a Scribd company logo
Redux	Deep	Dive
Destructuring an	Object
1. const newState = {
2. ...state,
3. key1: value1,
4. key2: value2
5. }
1. const newState = _.extend(
2. {},
3. state,
4. {
5. key1, value1,
6. key2, value2
7. }
8. )
Agenda
1. Redux	Basics	(Brief)
2. Enhancers,	Middlewares
3. Problems
1. Typos,	Types,	Tree	Shaking,	Chunking
2. Partial	but	failing	Solutions
4. Solution
React’s Philosophy
Props	/	
State
VDOM DOM
render
Pure	Function
React
React	Philosophy
Events
Props	/	
State
VDOM DOM
render
Pure	Function
ReactsetState
As	your	App	grows,	it	
becomes	difficult	to	
manage	using	setState
Enter	Redux
Redux Store
State
Component
Action
Reducers
Provide	store	at	root	level
1. import { createStore } from 'redux'
2. import { Provider } from 'react-redux';
3.
4. import reducer from './reducers';
5.
6. const store = createStore(reducer);
7.
8. export default () => {
9. return (
10. <Provider store={store}>
11. <App>
12. </Provider>
13. )
14. };
Store’s	State	passed	to	Components
1. import { connect } from 'react-redux'
2.
3. class HeaderComponent extends React.Component {
4. render () {
5. return (
6. <h1> {this.props.user} </h1>
7. )
8. }
9. }
10.
11. const mapStateToProps = (state) => ({
12. user: state.user
13. });
14.
15. export default connect(mapStateToProps)(HeaderComponent);
Dispatch	an	Action
1. class Counter extends React.Component {
2. onClick = (e) => {
3. this.props.dispatch({
4. type: 'COUNTER_CLICKED',
5. payload: e.currentTarget.data.id
6. })
7. }
8. render () {
9. return <div
10. onClick={this.onClick(e)}
11. data-id={this.props.id}
12. />
13. }
14. }
Reducer
1. export default function reducer(state, action) {
2. switch (action.type) {
3. case 'COUNTER_CLICKED':
4. return {
5. ...state,
6. counterClicked: true
7. }
8. ...
9. default:
10. return state;
11. }
12.}
Redux Store
State
Component
Action
Reducers
Time	to	Level	Up
Enhancers
createStore => newCreateStore
Enhancers	wrap	around	createStore
enhancer
createStore
1. function enhancer (createStore) {
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.}
Sample	Enhancer	which	logs	on	every	getState()
1. function enhancer (createStore) {
2. return (...args) => {
3.
4.
5.
6.
7.
8.
9.
10.
11. }
12.}
Sample	Enhancer	which	logs	on	every	getState()
1. function enhancer (createStore) {
2. return (...args) => {
3. const store = createStore(...args)
4. return {
5. ...store,
6.
7.
8.
9.
10. }
11. }
12.}
Sample	Enhancer	which	logs	on	every	getState()
1. function enhancer (createStore) {
2. return (...args) => {
3. const store = createStore(...args)
4. return {
5. ...store,
6. getState: () => {
7. console.log('getState was called')
8. return store.getState()
9. }
10. }
11. }
12.}
Sample	Enhancer	which	logs	on	every	getState()
How	to	use	Enhancers
1. const store = enchancer(createStore)(reducer)
OR
1. const store = createStore(reducer, enhancer)
Multiple	Enhancers
1. const store = enhancer1(enhancer2(createStore))(reducer)
OR
1. const store = compose(enhacer1, enhancer2)(createStore)(reducer)
OR
1. const store = createStore(reducer, compose(enchancer1, enhancer2))
(( )))compose( first , second , third )( ...args( )
Art	by	C
Debugging	– Redux Dev	Tools
applyMiddleware
1. export default function applyMiddleware(...middlewares) {
2. return (createStore) => (...args) => {
3. const store = createStore(...args)
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14. }
15. }
applyMiddleware is	an	Enhancer
1. export default function applyMiddleware(...middlewares) {
2. return (createStore) => (...args) => {
3. const store = createStore(...args)
4. let dispatch = () => {}
5. const newStore = {
6. ...store,
7. dispatch: (...action) => dispatch(...action)
8. }
9.
10.
11.
12.
13. return newStore
14. }
15. }
applyMiddleware is	an	Enhancer
1. export default function applyMiddleware(...middlewares) {
2. return (createStore) => (...args) => {
3. const store = createStore(...args)
4. let dispatch = () => {}
5. const newStore = {
6. ...store,
7. dispatch: (...action) => dispatch(...action)
8. }
9.
10. const chain = middlewares.map(middleware => middleware(newStore)) // init with store
11. dispatch = compose(...chain)(store.dispatch) // load with next dispatch
12.
13. return newStore
14. }
15. }
Redux Middlewares
store => nextMiddleware => action => nextMiddleware(action)
Redux	Thunk
1. const thunk = store => next => action => {
2. if (typeof action === 'function') {
3. return action(store.dispatch, store.getState)
4. }
5.
6. return next(action)
7. }
Redux	Thunk Usage
1. function doSomethingAction (dispatch) {
2. dispatch((dispatch, getState) => {
3.
4.
5.
6.
7.
8. })
9. }
Redux	Thunk Usage
1. function doSomethingAction (dispatch) {
2. dispatch((dispatch, getState) => {
3. const state = getState()
4. dispatch({
5. type: 'DO_SOMETHING',
6. payload: state.user
7. })
8. })
9. }
Non	DOM	Side	Effects
1. const setTitle = store => next => action => {
2. const getState = store.getState
3.
4. const oldTitle = getState().title
5. next(action)
6. const newTitle = getState().title
7.
8. if (oldTitle !== newTitle) {
9. document.title = newTitle
10. }
11. return action
12.}
Tracking	Breadcrumbs	/	User	Flow
1. import Raven from '../helpers/sentry’
2.
3. const trackBreadCrumb = () => next => action => {
4. Raven.captureBreadcrumb({
5. category: 'redux'
6. message: action.type
7. })
8. return next(action)
9. }
Sentry	Error	Logs	Breadcrumbs
How	to	use	these	middlewares
1. import { createStore, applyMiddleware } from 'redux'
2.
const store = createStore(
3. reducer,
4. compose(
5. applyMiddleware(
6. thunk,
7. setTitle,
8. trackBreadcrumb
9. ),
10. window.__REDUX_DEVTOOLS_EXTENSION__()
11. )
12. )
Redux-sagas
For	more	info	attend	Preeti’s talk	after	lunch.
My	Problems
My	Problems
^
1.	Typos
Typos
1. dispatch({
2. type: 'DO_SOEMTHING_AMAMAZING'
3. })
export const AMAZING_ACTION = 'AMAZING_ACTION’ // actionsList.js
1. import {AMAZING_ACTION} from
'./actionsList’
2.
3. dispatch({
4. type: AMAZING_ACTION
5. })
1. import {AMAZING_ACTION} from
'../actionsList’
2.
3. export function reducer (state, action) {
4. switch (action.type) {
5. case AMAZING_ACTION:
6. ...
7. }
8. }
// actionsList.js
1. export const AMAZING_ACTION = 'AMAZING_ACTION'
2. export const AWESOME_ACTION = 'AWESOME_ACTION'
3. export const FANTASTIC_ACTION = 'FANTASTIC_ACTION'
4. export const MINDBLOWING_ACTION = 'MINDBLOWING_ACTION'
5. export const ACTION_ACTION = 'ACTION_ACTION'
6. export const DRAMA_ACTION = 'DRAMA_ACTION'
7. export const AMAZING_ACTION_2 = 'AMAZING_ACTION_2'
8. export const AWESOME_ACTION_2 = 'AWESOME_ACTION_2'
9. export const FANTASTIC_ACTION_2 = 'FANTASTIC_ACTION_2'
10. export const MINDBLOWING_ACTION_2 = 'MINDBLOWING_ACTION_2'
11. export const ACTION_ACTION_2 = 'ACTION_ACTION_2'
12. export const DRAMA_ACTION_2 = 'DRAMA_ACTION_2'
13. export const AMAZING_ACTION_3 = 'AMAZING_ACTION_3'
14. export const AWESOME_ACTION_3 = 'AWESOME_ACTION_3'
15. export const FANTASTIC_ACTION_3 = 'FANTASTIC_ACTION_3'
16. export const MINDBLOWING_ACTION_3 = 'MINDBLOWING_ACTION_3'
17. export const ACTION_ACTION_3 = 'ACTION_ACTION_3'
18. export const DRAMA_ACTION_3 = 'DRAMA_ACTION_3’
Need	to	add	in	3	places	instead	of	2	😩
1.Actions
2.Reducers
3.ActionsList
Redux Deep Dive - ReactFoo Pune 2018
2.	Types
Types	- Action
1. dispatch({
2. type: 'ACTION_DRAMA',
3. payload: 8
4. })
Types	- Action
1. dispatch({
2. type: 'ACTION_DRAMA',
3. payload: {
4. id: 8,
5. rating: 9.5
6. }
7. })
Types	- Reducer
1. function reducer (state, action) {
2. switch (action.type) {
3. case 'ACTION_DRAMA':
4. const id = action.payload
5.
6. ...
7.
8. }
9. }
Types	- Reducer
1. function reducer (state, action) {
2. switch (action.type) {
3. case 'ACTION_DRAMA':
4. const {id, rating} = action.payload
5.
6. ...
7.
8. }
9. }
In	another	location,	you	forgot
1. dispatch({
2. type: 'ACTION_DRAMA',
3. payload: 8
4. })
How	are	you	going	to	grep /	search	this?
1. const ACTION = 'ACTION_'
2. dispatch({
3. type: ACTION + 'DRAMA',
4. payload: 8
5. })
Image	by	Dave	Dugdale of	www.learningDSLRVideo.com
Redux Deep Dive - ReactFoo Pune 2018
Redux Deep Dive - ReactFoo Pune 2018
Redux Deep Dive - ReactFoo Pune 2018
Action	Types
1. export type DramaAction = {
2. type: 'DRAMA_ACTION',
3. payload: {
4. id: number,
5. rating: number
6. },
7. };
8.
9. export type ActionAction = {
10. type: 'ACTION_ACTION',
11. payload: number,
12. };
13.
14. export type Action = DramaAction | ActionAction;
Store	Types
1. import type {
2. Store as ReduxStore,
3. Dispatch as ReduxDispatch,
4. } from 'redux';
5. import type { Action } from './Action';
6. import type { State } from './State';
7.
8. export type Store = ReduxStore<State, Action>;
9.
10. export type GetState = () => State;
11.
12. export type Dispatch = ReduxDispatch<Action>
13.
Action
1. import type {Dispatch} from './types/Store'
2.
export function dramaAction(dispatch: Dispatch, id: number, rating: number) {
3. return dispatch({
4. type: 'DRAMA_ACTION',
5. payload: { id, rating }
6. });
7. }
8.
export function actionAction(dispatch: Dispatch, id: number) {
9. return dispatch({
10. type: 'ACTION_ACTION',
11. payload: id
12. })
13. }
Need	to	maintain	3	places	instead	of	2	😩
1.Actions
2.Reducers
3.ActionTypes
Redux Deep Dive - ReactFoo Pune 2018
Too	much	work,	must	find	hack.
1. export type FallbackAction = {
2. type: string,
3. [string]: any
4. }
5.
6. export type Action = DramaAction | ActionAction | FallbackAction;
3.	Tree	Shaking
Removing	unused	code	during	build	or	minification
Long	list	of	Switch	Case	in	Reducer
1. export default function reducer(state, action) {
2. switch (action.type) {
3. case 'DRAMA_ACTION':
4. return {
5. ...state,
6. movie: {id: action.id, rating: action.rating}
7. }
8. case 'ACTION_ACTION':
9. return {
10. ...state,
11. movie: {id: action.id}
12. }
13. default:
14. return state;
15. }
16. }
Tree	Shaking	Dead	Code
becomes	difficult
4.	Route	Specific	Code	Splitting	
Reducers
Pinterest’s	Chunks
Possibly	even	Reducers
Pinterest’s	Chunks
Route	Specific	Reducers
store.replaceReducer
1. import globalReducer from './reducers/globalReducer'
2.
function onRouteChange (newReducer) {
3. store.replaceReducer(
4. mergeReducer(
5. globalReducer,
6. newReducer
7. )
8. )
9. }
How	to	go	ahead	with	Route	Specific	Reducers
1. Each	Chunk	defines	its	reducer
2. Before	component	is	rendered,	call	
store.replaceReducer
Problems	with	this	approach
1. Difficult	to	migrate.
2. Difficult	to	ensure	that	the	correct	reducer	will	be	
available	when	you	dispatch	an	action.
Photo	by	Riccardo	Annandale on	Unsplash
Dispatch	your	reducer
Redux Deep Dive - ReactFoo Pune 2018
Dispatch	your	reducer	instead	of	type
1. import {ACTION_DRAMA} from './dramaReducers'
2.
export function dramaAction (dispatch, id, rating) {
3. dispatch({
4. reducer: ACTION_DRAMA,
5. payload: {
6. id,
7. rating
8. }
9. })
10.}
Reducer
1. export function ACTION_DRAMA (state, payload) {
2. return {
3. ...state,
4. movie: {
5. id: payload.id,
6. rating: payload.rating
7. }
8. }
9. }
But	does	it	fix	my	problems?
1.	Typos
1. import { ACTION_DRAMA } from './dramaReducers'
2.
export function dramaAction (dispatch, id, rating) {
3. dispatch({
4. reducer: ACTION_DRAMA,
5. payload: {
6. id,
7. rating
8. }
9. })
10.}
2.	Types
1. import type { State } from './State';
2.
3. export type Reducer<P> = (
4. state: State,
5. payload: P
6. ) => State;
7.
8. export type Action<P> = {
9. reducer: Reducer<P>,
10. payload: P
11. }
12.
13. export type Dispatch = <T>(action: Action<T>) => void
2.	Types	- Reducer
1. export function INCREMENT_COUNTER (state: State, payload: number): State {
2. return {
3. ...state,
4. counter: state.counter + payload
5. }
6. }
2.	Types	- Action
1. export function increment(dispatch: Dispatch,
amount: number) {
2. return dispatch({
3. reducer: INCREMENT_COUNTER,
4. payload: 1,
5. });
6. }
7.
2.	Types	- IDE	(Nuclide)	throws	errors
3.	Tree	Shaking	Unused	Reducer	Code
1. import { ACTION_DRAMA } from './dramaReducers'
2.
export function dramaAction (dispatch, id, rating) {
3. dispatch({
4. reducer: ACTION_DRAMA,
5. payload: {
6. id,
7. rating
8. }
9. })
10.}
3.	Tree	Shaking	Unused	Reducer	Code
1. https://github.com/kentcdodds/webpack-tree-shaking-exports
4.	Code	Splitting	across	Routes	- Before
Entry
Store
Reducers Middlewares
Component
Actions
4.	Code	Splitting	across	Routes	- After
Entry
Store
Middlewares
Component
Actions
Reducers
How	to	get	this	to	work?
1. const dispatchReducerMiddleware = () => next => action => {
2. if (
3. typeof action === 'object' &&
4. typeof action.reducer === 'function'
5. ) {
6. action = {
7. ...action,
8. type: action.reducer.name
9. }
10. }
11. return next(action)
12.}
What	about	the	reducer	passed	to	createStore?
1. function reducer (state, action) {
2. if (action.reducer) {
3. return action.reducer(state, action.payload)
4. }
5. return state
6. }
How	to	use	with	Redux	Devtools
1. const store = createStore(
2. reducer,
3. compose(
4. applyMiddleware(
5. ...middlewares,
6. dispatchReducerMiddleware
7. ),
8. window.__REDUX_DEVTOOLS_EXTENSION__()
9. )
10.)
Redux Deep Dive - ReactFoo Pune 2018
combineReducers
Left	as	an	exercise	to	the	Reader	of
https://github.com/azizhk/react-boilerplate/pull/1
Codemod
https://gist.github.com/azizhk/b4f9f5e45055a25bd28eef56540714e4
Takeaway
1. Write	your	enhancer	for	your	problems.
2. Write	codemods for	migrations
Aziz	Khambati
/azizhk110
/@azizhk

More Related Content

PDF
Mining Version Histories to Guide Software Changes
PDF
eROSE: Guiding programmers in Eclipse
PPTX
Operator overload rr
PPTX
React responsively, render responsibly - react meetup
PDF
How History Justifies System Architecture (or Not)
KEY
Djangocon11: Monkeying around at New Relic
ODP
Pruebas unitarias con django
DOCX
Exercícios Netbeans - Vera Cymbron
Mining Version Histories to Guide Software Changes
eROSE: Guiding programmers in Eclipse
Operator overload rr
React responsively, render responsibly - react meetup
How History Justifies System Architecture (or Not)
Djangocon11: Monkeying around at New Relic
Pruebas unitarias con django
Exercícios Netbeans - Vera Cymbron

What's hot (18)

PDF
Optimization in django orm
PDF
Symfony2 - from the trenches
PPTX
JavaScript Proven Practises
PDF
Knock, knock, who is there? Doze.
PPTX
Hibernate
PPTX
Unit 4
PPTX
Behavioral pattern 4
DOC
White Paper On ConCurrency For PCMS Application Architecture
PPTX
Secret unit testing tools no one ever told you about
DOC
OR Mapping- nhibernate Presentation
DOC
Converting Db Schema Into Uml Classes
DOC
To Study The Tips Tricks Guidelines Related To Performance Tuning For N Hib...
PDF
The art of reverse engineering flash exploits
PDF
What's new in Doctrine
PDF
Swift for TensorFlow - CoreML Personalization
DOCX
Advance Java Programs skeleton
PDF
Volley lab btc_bbit
Optimization in django orm
Symfony2 - from the trenches
JavaScript Proven Practises
Knock, knock, who is there? Doze.
Hibernate
Unit 4
Behavioral pattern 4
White Paper On ConCurrency For PCMS Application Architecture
Secret unit testing tools no one ever told you about
OR Mapping- nhibernate Presentation
Converting Db Schema Into Uml Classes
To Study The Tips Tricks Guidelines Related To Performance Tuning For N Hib...
The art of reverse engineering flash exploits
What's new in Doctrine
Swift for TensorFlow - CoreML Personalization
Advance Java Programs skeleton
Volley lab btc_bbit
Ad

Similar to Redux Deep Dive - ReactFoo Pune 2018 (20)

PDF
Understanding redux
PDF
Intro to Redux | DreamLab Academy #3
PDF
Egghead redux-cheat-sheet-3-2-1
PPTX
Redux workshop
PPTX
Redux training
PPTX
PPTX
Maintaining sanity in a large redux app
PDF
State of the state
PDF
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
PDF
Redux js
PDF
How to use redux with react hooks in react native application
PDF
Getting started with React and Redux
PDF
Redux essentials
PDF
Materi Modern React Redux Power Point.pdf
PDF
Redux tutorial - intro to Redux by GetLittleTech
PPTX
an Introduction to Redux
PDF
React Redux Interview Questions PDF By ScholarHat
PDF
React Redux Interview Questions PDF By ScholarHat
PPTX
React js programming concept
PPTX
Damian Kmiecik - Road trip with Redux
Understanding redux
Intro to Redux | DreamLab Academy #3
Egghead redux-cheat-sheet-3-2-1
Redux workshop
Redux training
Maintaining sanity in a large redux app
State of the state
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Redux js
How to use redux with react hooks in react native application
Getting started with React and Redux
Redux essentials
Materi Modern React Redux Power Point.pdf
Redux tutorial - intro to Redux by GetLittleTech
an Introduction to Redux
React Redux Interview Questions PDF By ScholarHat
React Redux Interview Questions PDF By ScholarHat
React js programming concept
Damian Kmiecik - Road trip with Redux
Ad

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
DOCX
573137875-Attendance-Management-System-original
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
OOP with Java - Java Introduction (Basics)
PDF
composite construction of structures.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Construction Project Organization Group 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
PPT on Performance Review to get promotions
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Sustainable Sites - Green Building Construction
Welding lecture in detail for understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
573137875-Attendance-Management-System-original
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
OOP with Java - Java Introduction (Basics)
composite construction of structures.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Internet of Things (IOT) - A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Construction Project Organization Group 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT on Performance Review to get promotions
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Geodesy 1.pptx...............................................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Sustainable Sites - Green Building Construction

Redux Deep Dive - ReactFoo Pune 2018