SlideShare a Scribd company logo
1 of 50
Download to read offline
React, Redux and Building
Applications that Scale
BY KRISTOF VAN MIEGEM
Co-Founder Codifly
Agenda
• Managing Application State
• Embracing Immutable Data Structures
• A Project Structure that Works
Managing Application State
With Redux
Example 1: Counter
Example 1: Counter
2 3
Example 2: Multiple Counters
Example 2: Multiple Counters
[ 1 ] [ 1, 4 ] [ 2, 4, 1 ]
Example 3: TodoMVC
Example 3: TodoMVC
{
todos: [
{ completed: true,
id: 0,
text: ‘Get a girfriend’ },
{ completed: true,
id: 1,
text: ‘Kiss her’ },
{ completed: false,
id: 2,
text: ‘Rule the world’ },
],
visibilityFilter: ‘SHOW_ALL’
}
State is Read-Only
The only way to change the state is to emit an
Action:
• A plain JavaScript that describes what
happened.
• Expresses an intent to change state
Example 1: Counter
{ type: ‘INC’ }
Example 2: Multiple Counters
Example 2: Multiple Counters
{ type: ‘INC’, counter: 0 }
{ type: ‘INC’, counter: 1 }
{ type: ‘INC’, counter: 2 }
{ type: ‘ADD_COUNTER’ }
Action Creators
function inc (counter) {
return { type: 'INC', counter };
}
function addCounter () {
return { type: 'ADD_COUNTER' };
}
State Changes are Made with Pure Functions
To specify how the state tree is transformed by
actions, you write reducers:
• Pure functions
• that take the previous state and an
action, and return the next state
reducer(state, action)
= reducer(state, action)
State Changes are Made with Pure Functions
Example: Counter
const counter = (state = 0, action) => {
switch (action.type) {
case 'INC':
return state + 1;
default:
return state;
}
};
Bringing it All Together: The Store
The store is responsible for:
• Containing the current state of the app
• Allowing state updates
• Notification of listeners
Store creation
let store = createStore(reducer)
Triggering changes: dispatching actions
store.dispatch(actionCreator(…))
Listening for state changes
store.subscribe(() =>
console.log(store.getState())
)
Bringing it All Together: The Store
Full Example
// Actions
const inc = () => ({ type: 'INC' });
const dec = () => ({ type: 'DEC' });
// Reducer
const counter = (state = 0, action) => {
switch (action.type) {
case ‘INC': return state + 1;
case ‘DEC': return state - 1;
default: return state;
}
};
Full Example
// View
class Counter extends React.Component {
render () {
return (
<div>
<h1>{this.props.value}</h1>
<button onClick={this.props.onIncrement}>
+
</button>
<button onClick={this.props.onDecrement}>
-
</button>
</div>
);
}
}
Full Example
// Store
const store = Redux.createStore(counter);
// Rendering
const render = () => {
ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() => store.dispatch(inc())}
onDecrement={() => store.dispatch(dec())}
/>,
document.getElementById('container')
);
};
render();
store.subscribe(render); https://jsfiddle.net/pxktc6pv/
Reselect
• Selectors get or compute data
• Selectors are composable
• Selectors are performant
Selectors
import { createSelector } from 'reselect'
const productsSelector =
state => state.productsInBasket
const vatPercentageSelector =
state => state.vatPercentage
const totalSelector = createSelector(
productsSelector,
vatPercentageSelector,
(products, vatPercentage) =>
products.reduce(
(acc, product) => acc + product.price, 0)
* (1 + vatPercentage)
)
Thunks
function inc () {
return (dispatch) => {
setTimeout(() => {
dispatch({ type: 'INC' });
}, 5000);
};
}
Embracing Immutable
Data Structures
For improved reasoning and performance
3 + 5 = 8
Numbers are Immutable
Arrays are Mutable
var a = [3];
a.push(5);
console.log(a);
Hypothetical Immutable Array
var a = [3];
a.push(5);
console.log(a);
var a = [3]i;
var a2
= a.push(4);
console.log(a);
console.log(a2);
Slide title https://facebook.github.io/immutable-js/
Immutable List
var a = [3]i;
var a2
= a.push(4);
console.log(a);
console.log(a2);
var l = List([3]);
var l2
= list.push(4);
console.log(l.toJS());
console.log(l2.toJS());
Updating Immutable Data
(1) Deep clone
(2) Perform changes
(3) Return result
Conceptually, the update process
Structural Sharing
1
2 3
4 6 75
8
9
Structural Sharing
1
2 3
4 6 75
8
9
Structural Sharing
1
2 3
4 6 75
8
9
10
Structural Sharing
1
2 3
4 6 75
8
9
10
11
Get and Set
const map1 = Immutable.Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 4);
console.log(map2.count()); // 3
console.log(map1.get('b')); // 2
console.log(map2.get('b')); // 4
const list1 = Immutable.List([ 1, 2 , 3 ]);
const list2 = list1.set(1, 4);
console.log(list2.count()); // 3
console.log(list1.get(1)); // 2
console.log(list2.get(1)); // 4
Nested Data Structures
var nested1 =
Immutable.fromJS({ a: { b: [ 1, 2, 3 ] } });
var nested2 =
nested1.setIn(['a', 'b', 1], 4);
console.log(nested1.getIn(['a', 'b', 1])); // 2
console.log(
nested2.getIn(['a', ‘b’]).toJS()); // [ 1, 2, 3 ]
console.log(nested2.getIn(['a', 'b', 1])); // 4
A Project Structure
that Works
Best practices at Codifly
“EditorConfig helps developers define and
maintain consistent coding styles between
different editors and IDEs.”

http://editorconfig.org/
Editorconfig
Editorconfig
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
function gcd (a, b) {
if (a == 0)
return b;
else {
return gcd(b % a, a);
}
}
ESLint
function gcd (a, b) {
if (a == 0)
return b;
else {
return gcd(b % a, a);
}
}
ESLint
2:3 error Expected { after 'if' condition
2:9 error Expected '===' and instead saw '=='
4:8 warning Unexpected 'else' after 'return'
8:1 warning Block must not be padded by blank lines
function gcd (a, b) {
if (a === 0) {
return b;
}
return gcd(b % a, a);
}
ESLint
ESLint
• Static Code Analysis
• “Agenda Free”
• Pluggable Standalone Rules

http://eslint.org/docs/rules/
• Customization via .eslintrc
Bringing it All Together
/src
/actions

/constants
/components
/reducers
/selectors
index.html
index.js
.babelrc
.editorconfig
.eslintrc
.gitignore
package.json
webpack.config.babel.js
Let’s script!
Getting our hands dirty with Redux
Slide title https://lpnpz04lxl.codesandbox.io/
Let’s script!
• Go to https://codesandbox.io/s/23lp02ql9n
/src
/actions
counterActions.js
/components
ClickButton.js
Counter.js
/reducers
counterReducer.js

index.js
/selectors
counterSelectors.js
index.js
Ready,
Set,
Code!
Solution: https://codesandbox.io/s/xkmq2p11w
Looking for
a challenge?
We are hiring!
Check out: https://codifly.be/careers

More Related Content

What's hot

Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
DAYANA RETO
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
Darwin Durand
 

What's hot (20)

Rxjs vienna
Rxjs viennaRxjs vienna
Rxjs vienna
 
Data20161007
Data20161007Data20161007
Data20161007
 
Investigation of Transactions in Cassandra
Investigation of Transactions in CassandraInvestigation of Transactions in Cassandra
Investigation of Transactions in Cassandra
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventas
 
Using Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your DataUsing Change Streams to Keep Up with Your Data
Using Change Streams to Keep Up with Your Data
 
Watch out: Observables are here to stay
Watch out: Observables are here to stayWatch out: Observables are here to stay
Watch out: Observables are here to stay
 
Uni2
Uni2Uni2
Uni2
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
Investigation of Transactions in Cassandra
Investigation of Transactions in CassandraInvestigation of Transactions in Cassandra
Investigation of Transactions in Cassandra
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY DOT NET LAB PROGRAM PERIYAR UNIVERSITY
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
 
Receipt processing with Google Cloud Platform and the Google Assistant
Receipt processing with Google Cloud Platform and the Google AssistantReceipt processing with Google Cloud Platform and the Google Assistant
Receipt processing with Google Cloud Platform and the Google Assistant
 
Building Apps with Flutter - Hillel Coren, Invoice Ninja
Building Apps with Flutter - Hillel Coren, Invoice NinjaBuilding Apps with Flutter - Hillel Coren, Invoice Ninja
Building Apps with Flutter - Hillel Coren, Invoice Ninja
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
Rxjs ppt
Rxjs pptRxjs ppt
Rxjs ppt
 
AngularJS: what is underneath the hood
AngularJS: what is underneath the hood AngularJS: what is underneath the hood
AngularJS: what is underneath the hood
 
Clojure functions examples
Clojure functions examplesClojure functions examples
Clojure functions examples
 
Data in Motion: Streaming Static Data Efficiently 2
Data in Motion: Streaming Static Data Efficiently 2Data in Motion: Streaming Static Data Efficiently 2
Data in Motion: Streaming Static Data Efficiently 2
 

Similar to 2018 02-22 React, Redux & Building Applications that Scale | Redux

Similar to 2018 02-22 React, Redux & Building Applications that Scale | Redux (20)

Ngrx slides
Ngrx slidesNgrx slides
Ngrx slides
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
Understanding redux
Understanding reduxUnderstanding redux
Understanding redux
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql Features
 
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
A Practical Approach to Building a Streaming Processing Pipeline for an Onlin...
 
Redux Deep Dive - ReactFoo Pune 2018
Redux Deep Dive - ReactFoo Pune 2018Redux Deep Dive - ReactFoo Pune 2018
Redux Deep Dive - ReactFoo Pune 2018
 
Damian Kmiecik - Road trip with Redux
Damian Kmiecik - Road trip with ReduxDamian Kmiecik - Road trip with Redux
Damian Kmiecik - Road trip with Redux
 
Battle of React State Managers in frontend applications
Battle of React State Managers in frontend applicationsBattle of React State Managers in frontend applications
Battle of React State Managers in frontend applications
 
Event Sourcing - what could go wrong - Devoxx BE
Event Sourcing - what could go wrong - Devoxx BEEvent Sourcing - what could go wrong - Devoxx BE
Event Sourcing - what could go wrong - Devoxx BE
 
Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]Lean React - Patterns for High Performance [ploneconf2017]
Lean React - Patterns for High Performance [ploneconf2017]
 
Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
 
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB.local DC 2018: Scaling Realtime Apps with Change StreamsMongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
MongoDB.local DC 2018: Scaling Realtime Apps with Change Streams
 
Redux vs Alt
Redux vs AltRedux vs Alt
Redux vs Alt
 
¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?¿Cómo de sexy puede hacer Backbone mi código?
¿Cómo de sexy puede hacer Backbone mi código?
 
Cassandra v3.0 at Rakuten meet-up on 12/2/2015
Cassandra v3.0 at Rakuten meet-up on 12/2/2015Cassandra v3.0 at Rakuten meet-up on 12/2/2015
Cassandra v3.0 at Rakuten meet-up on 12/2/2015
 
Getting Reactive with Cycle.js and xstream
Getting Reactive with Cycle.js and xstreamGetting Reactive with Cycle.js and xstream
Getting Reactive with Cycle.js and xstream
 
Famo.us: From Zero to UI
Famo.us: From Zero to UIFamo.us: From Zero to UI
Famo.us: From Zero to UI
 
Recoil at Codete Webinars #3
Recoil at Codete Webinars #3Recoil at Codete Webinars #3
Recoil at Codete Webinars #3
 
Introduction to-mongo db-execution-plan-optimizer-final
Introduction to-mongo db-execution-plan-optimizer-finalIntroduction to-mongo db-execution-plan-optimizer-final
Introduction to-mongo db-execution-plan-optimizer-final
 

Recently uploaded

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Recently uploaded (20)

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 

2018 02-22 React, Redux & Building Applications that Scale | Redux

  • 1. React, Redux and Building Applications that Scale BY KRISTOF VAN MIEGEM Co-Founder Codifly
  • 2. Agenda • Managing Application State • Embracing Immutable Data Structures • A Project Structure that Works
  • 7. Example 2: Multiple Counters [ 1 ] [ 1, 4 ] [ 2, 4, 1 ]
  • 9. Example 3: TodoMVC { todos: [ { completed: true, id: 0, text: ‘Get a girfriend’ }, { completed: true, id: 1, text: ‘Kiss her’ }, { completed: false, id: 2, text: ‘Rule the world’ }, ], visibilityFilter: ‘SHOW_ALL’ }
  • 10. State is Read-Only The only way to change the state is to emit an Action: • A plain JavaScript that describes what happened. • Expresses an intent to change state
  • 13. Example 2: Multiple Counters { type: ‘INC’, counter: 0 } { type: ‘INC’, counter: 1 } { type: ‘INC’, counter: 2 } { type: ‘ADD_COUNTER’ }
  • 14. Action Creators function inc (counter) { return { type: 'INC', counter }; } function addCounter () { return { type: 'ADD_COUNTER' }; }
  • 15. State Changes are Made with Pure Functions To specify how the state tree is transformed by actions, you write reducers: • Pure functions • that take the previous state and an action, and return the next state
  • 16. reducer(state, action) = reducer(state, action) State Changes are Made with Pure Functions
  • 17. Example: Counter const counter = (state = 0, action) => { switch (action.type) { case 'INC': return state + 1; default: return state; } };
  • 18. Bringing it All Together: The Store The store is responsible for: • Containing the current state of the app • Allowing state updates • Notification of listeners
  • 19. Store creation let store = createStore(reducer) Triggering changes: dispatching actions store.dispatch(actionCreator(…)) Listening for state changes store.subscribe(() => console.log(store.getState()) ) Bringing it All Together: The Store
  • 20. Full Example // Actions const inc = () => ({ type: 'INC' }); const dec = () => ({ type: 'DEC' }); // Reducer const counter = (state = 0, action) => { switch (action.type) { case ‘INC': return state + 1; case ‘DEC': return state - 1; default: return state; } };
  • 21. Full Example // View class Counter extends React.Component { render () { return ( <div> <h1>{this.props.value}</h1> <button onClick={this.props.onIncrement}> + </button> <button onClick={this.props.onDecrement}> - </button> </div> ); } }
  • 22. Full Example // Store const store = Redux.createStore(counter); // Rendering const render = () => { ReactDOM.render( <Counter value={store.getState()} onIncrement={() => store.dispatch(inc())} onDecrement={() => store.dispatch(dec())} />, document.getElementById('container') ); }; render(); store.subscribe(render); https://jsfiddle.net/pxktc6pv/
  • 23. Reselect • Selectors get or compute data • Selectors are composable • Selectors are performant
  • 24. Selectors import { createSelector } from 'reselect' const productsSelector = state => state.productsInBasket const vatPercentageSelector = state => state.vatPercentage const totalSelector = createSelector( productsSelector, vatPercentageSelector, (products, vatPercentage) => products.reduce( (acc, product) => acc + product.price, 0) * (1 + vatPercentage) )
  • 25. Thunks function inc () { return (dispatch) => { setTimeout(() => { dispatch({ type: 'INC' }); }, 5000); }; }
  • 26. Embracing Immutable Data Structures For improved reasoning and performance
  • 27. 3 + 5 = 8 Numbers are Immutable
  • 28. Arrays are Mutable var a = [3]; a.push(5); console.log(a);
  • 29. Hypothetical Immutable Array var a = [3]; a.push(5); console.log(a); var a = [3]i; var a2 = a.push(4); console.log(a); console.log(a2);
  • 31. Immutable List var a = [3]i; var a2 = a.push(4); console.log(a); console.log(a2); var l = List([3]); var l2 = list.push(4); console.log(l.toJS()); console.log(l2.toJS());
  • 32. Updating Immutable Data (1) Deep clone (2) Perform changes (3) Return result Conceptually, the update process
  • 36. Structural Sharing 1 2 3 4 6 75 8 9 10 11
  • 37. Get and Set const map1 = Immutable.Map({ a: 1, b: 2, c: 3 }); const map2 = map1.set('b', 4); console.log(map2.count()); // 3 console.log(map1.get('b')); // 2 console.log(map2.get('b')); // 4 const list1 = Immutable.List([ 1, 2 , 3 ]); const list2 = list1.set(1, 4); console.log(list2.count()); // 3 console.log(list1.get(1)); // 2 console.log(list2.get(1)); // 4
  • 38. Nested Data Structures var nested1 = Immutable.fromJS({ a: { b: [ 1, 2, 3 ] } }); var nested2 = nested1.setIn(['a', 'b', 1], 4); console.log(nested1.getIn(['a', 'b', 1])); // 2 console.log( nested2.getIn(['a', ‘b’]).toJS()); // [ 1, 2, 3 ] console.log(nested2.getIn(['a', 'b', 1])); // 4
  • 39. A Project Structure that Works Best practices at Codifly
  • 40. “EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs.”
 http://editorconfig.org/ Editorconfig
  • 41. Editorconfig root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true
  • 42. function gcd (a, b) { if (a == 0) return b; else { return gcd(b % a, a); } } ESLint
  • 43. function gcd (a, b) { if (a == 0) return b; else { return gcd(b % a, a); } } ESLint 2:3 error Expected { after 'if' condition 2:9 error Expected '===' and instead saw '==' 4:8 warning Unexpected 'else' after 'return' 8:1 warning Block must not be padded by blank lines
  • 44. function gcd (a, b) { if (a === 0) { return b; } return gcd(b % a, a); } ESLint
  • 45. ESLint • Static Code Analysis • “Agenda Free” • Pluggable Standalone Rules
 http://eslint.org/docs/rules/ • Customization via .eslintrc
  • 46. Bringing it All Together /src /actions
 /constants /components /reducers /selectors index.html index.js .babelrc .editorconfig .eslintrc .gitignore package.json webpack.config.babel.js
  • 47. Let’s script! Getting our hands dirty with Redux Slide title https://lpnpz04lxl.codesandbox.io/
  • 48. Let’s script! • Go to https://codesandbox.io/s/23lp02ql9n /src /actions counterActions.js /components ClickButton.js Counter.js /reducers counterReducer.js
 index.js /selectors counterSelectors.js index.js
  • 50. Looking for a challenge? We are hiring! Check out: https://codifly.be/careers