SlideShare a Scribd company logo
REACT AND REDUX
LET'S DISCOVER…
Mathieu Savy | @mrblackus
LET'S DISCOVER REACT AND REDUX
WHAT IS REACT?
▸ JS library to build User Interface (UI)
▸ Everything is Component
▸ Unidirectional data-flow (from parent to child)
▸ Virtual DOM
▸ NO MORE! (no controller, no router, no filter, etc.)
LET'S DISCOVER REACT AND REDUX
LET'S DISCOVER REACT AND REDUX
AppComponent
LET'S DISCOVER REACT AND REDUX
AppComponent
SearchBarComponent
LET'S DISCOVER REACT AND REDUX
AppComponent
SearchBarComponent
SortBarComponent
LET'S DISCOVER REACT AND REDUX
AppComponent
QuestionListComponent
SearchBarComponent
SortBarComponent
LET'S DISCOVER REACT AND REDUX
AppComponent
QuestionListComponent
QuestionComponent
SearchBarComponent
SortBarComponent
LET'S DISCOVER REACT AND REDUX
AppComponent
QuestionListComponent
QuestionComponent
LabelComponent
BadgeComponent
SearchBarComponent
SortBarComponent
LET'S DISCOVER REACT AND REDUX
COMPONENT
LET'S DISCOVER REACT AND REDUX
I'm pretty sure you can't do that.
COMPONENT
LET'S DISCOVER REACT AND REDUX
Anyway, you shouldn't.
Separation of concerns,
MVC, bla bla bla… 🤓
COMPONENT
LET'S DISCOVER REACT AND REDUX
And that?!
Anyway, you shouldn't.
Separation of concerns,
MVC, bla bla bla…
COMPONENT
LET'S DISCOVER REACT AND REDUX
PROPERTIES
▸ Used to pass values from a parent component to a child component
▸ Like HTML attributes
▸ Immutable values (can't be used to pass data from child to parent)
Will display : "Hey! Hello Mathieu!"
LET'S DISCOVER REACT AND REDUX
STATE
▸ Represent the internal state of a component
▸ State can be modified
▸ Must have an initial value
▸ Component is re-rendered if the state is modified
LET'S DISCOVER REACT AND REDUX
STATE
LET'S DISCOVER REACT AND REDUX
STATE
And if I want to choose
my initial state at
runtime?
🤔
LET'S DISCOVER REACT AND REDUX
STATE
Use props!
😃
LET'S DISCOVER REACT AND REDUX
LET'S TYPE CHECK OUR COUNTER
▸ We use TypeScript for type checking, so let's be sure our number is a number
(who knows…)
▸ Remember the <any, any> part of React.Component?
LET'S DISCOVER REACT AND REDUX
LET'S TYPE CHECK OUR COUNTER
Now, you can try to put
something else than a
number on our counter, it
won't work
LET'S DISCOVER REACT AND REDUX
LET'S TYPE CHECK OUR COUNTER
LET'S DISCOVER REACT AND REDUX
LET'S TYPE CHECK OUR COUNTER
🕵
LET'S DISCOVER REACT AND REDUX
Type checking = peace of mind
LET'S DISCOVER REACT AND REDUX
DATA FLOW
▸ Data flows from parent to children components
▸ To pass values to a child, we use props
▸ There only is a one way data-binding
Parent -> child
communication with property
LET'S DISCOVER REACT AND REDUX
DATA FLOW
🤔
But now, I want to mark an item as "done" when I click on it.
Props are immutable, so I can't do this.props.item.done = true;
How can I warn the parent to mark my item as "done" ?
❌
LET'S DISCOVER REACT AND REDUX
DATA FLOW
🤔
But now, I want to mark an item as "done" when I click on it.
Props are immutable, so I can't do this.props.item.done = true;
How can I warn the parent to mark my item as "done" ?
EVENTS
Data flows down, events flow up
LET'S DISCOVER REACT AND REDUX
DATA FLOW
1.On TodoList component, define an action we want to execute when clicking on
a TodoItem
2.Pass this action as a prop to every TodoItem
3.When click is triggered on TodoItem, call the action that was passed as prop
LET'S DISCOVER REACT AND REDUX
DATA FLOW
1.On TodoList component, define an action we want to execute when clicking on
a TodoItem
2.Pass this action as a prop to every TodoItem
3.When click is triggered on TodoItem, call the action that was passed as prop
DELEGATION
Let's discover React and Redux with TypeScript
Data
Data Events
LET'S DISCOVER REACT AND REDUX
VIRTUAL DOM
▸ DOM manipulations are (very) slow
▸ Use of Virtual DOM allows us to re-render the DOM only when necessary
▸ Re-render only needed subtrees (diff between old state and new state)
▸ Allow server-side rendering
▸ => Better performance
LET'S DISCOVER REACT AND REDUX
VIRTUAL DOM
Credit: http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
LET'S DISCOVER REACT AND REDUX
VIRTUAL DOM
Credit: http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
LET'S DISCOVER REACT AND REDUX
OKAY, THAT SEEMS COOL. BUT WHY?
▸ UI became predictable and deterministic
▸ Predictable => easier to understand and test
▸ Components are reusable and more maintainable
▸ Use of virtual DOM allows very good performances
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
Server response
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
Server response
Cached data
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
Server response
Cached data
Temporary form data
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
Server response
Cached dataUI state
Temporary form data
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
Server response
Cached dataUI state
Temporary form data
Loader during async tasks
LET'S DISCOVER REACT AND REDUX
Hey, it's okay for a todo-list. But what about a real application?
Server response
Cached dataUI state
Temporary form data
Loader during async tasks
Error message
LET'S DISCOVER REACT AND REDUX
State management is going to be a nightmare!
LET'S DISCOVER REACT AND REDUX
State management is going to be a nightmare!
😈
LET'S DISCOVER REACT AND REDUX
Flux Relay Redux
to the rescue
LET'S DISCOVER REACT AND REDUX
WHAT IS REDUX?
"Redux is a predictable state container for JavaScript apps."
http://redux.js.org
LET'S DISCOVER REACT AND REDUX
WHAT IS REDUX?
State container?
There is one unique state container called store. It contains your application state.
Same store = same application render
LET'S DISCOVER REACT AND REDUX
WHAT IS REDUX?
Predictable?
State is immutable
We can get a new state by dispatching actions
LET'S DISCOVER REACT AND REDUX
WHAT IS REDUX?
Predictable?
State is immutable
We can get a new state by dispatching actions
"Mark item 3 as read"
{

type: "MARK_READ",

itemId: 3

}
=>
LET'S DISCOVER REACT AND REDUX
WHAT IS REDUX?
Predictable?
Actions are consumed by reducers
Reducers take an action, the actual state and return a new state
function reducer(state, action) => newState
LET'S DISCOVER REACT AND REDUX
WHAT IS REDUX?
Predictable?
Reducers are pure functions.
Same parameters = same result. Always.
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
Let's build a counter (yeah, again)!
▸ We have two actions: increment and decrement
▸ If another (or an unknown) action is passed, do nothing (i.e. return the current
state)
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
The reducer
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
The reducer
🤔
Hum, on the first call, state will
be undefined, isn't it?
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
The reducer
ES6 default values to the rescue!

This is the way to define our app
initial state.
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
The store
We create the store by passing our reducer to Redux createStore() function
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
And we are done!
Yep, Redux is that simple. If you got this, you understood all the basics of Redux.
LET'S DISCOVER REACT AND REDUX
A LITTLE EXAMPLE?
Hey, don't trick me!

There's not a single line of React in your example.
😐
LET'S DISCOVER REACT AND REDUX
REDUX × REACT
▸ Use react-redux package
▸ Relies on presentational and container components
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: PRESENTATIO-WHAT?
Credit: http://redux.js.org/docs/basics/UsageWithReact.html
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: PRESENTATIONAL COMPONENTS
▸ Data from props
▸ Invoke callbacks (from props) to change data — Delegation
▸ Rarely have a state (in such case, just UI state, no data)
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: CONTAINER COMPONENTS
▸ Are generated (by react-redux) to work with a presentational component
▸ Generated from two functions that describes how to:
▸ map the state to child component props
▸ map dispatch calls to child components props
▸ Need Redux store passed as property
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: CONTAINER COMPONENTS
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: CONTAINER COMPONENTS
Presentational Component
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: CONTAINER COMPONENTS
Presentational Component
Counter component will have
count and onIncrement
properties
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: CONTAINER COMPONENTS
Presentational Component
Counter component will have
count and onIncrement
properties
Redux Store
LET'S DISCOVER REACT AND REDUX
REDUX × REACT: CONTAINER COMPONENTS
Passing store to every container components will rapidly become impossible to handle properly.
That's why react-redux exposes a Provider component that handles it for us.
LET'S DISCOVER REACT AND REDUX
A LITTLE MORE: ASYNC
Once again, that's cool for a todo-list, but what about async calls with Redux?
▸ See redux-thunk middleware
▸ Notion of async action, that dispatches actions (request start, request end, etc.)
More: http://redux.js.org/docs/advanced/AsyncActions.html
LET'S DISCOVER REACT AND REDUX
RESOURCES
▸ React doc: https://facebook.github.io/react/
▸ Holy Redux Bible: http://redux.js.org/index.html
Thanks for listening!
Questions?

More Related Content

What's hot (20)

PDF
Javascript
Vibhor Grover
 
PDF
07 java collection
Abhishek Khune
 
PPT
Javascript arrays
Hassan Dar
 
PPT
enums
teach4uin
 
PPT
Introduction to Javascript
Amit Tyagi
 
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
Hùng Nguyễn Huy
 
PPTX
Typescript ppt
akhilsreyas
 
PDF
Introduction to web programming with JavaScript
T11 Sessions
 
PPTX
what is functional component
manojbkalla
 
PDF
TypeScript
Saray Chak
 
PDF
Clean coding-practices
John Ferguson Smart Limited
 
PPTX
Object Oriented Programming In JavaScript
Forziatech
 
PDF
Clean code
Arturo Herrero
 
PDF
Asynchronous JavaScript Programming
Haim Michael
 
PDF
Java Collection framework
ankitgarg_er
 
PDF
Django
Amanpreet Singh
 
PPTX
Promises, promises, and then observables
Stefan Charsley
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
PDF
Introduction to gradle
NexThoughts Technologies
 
Javascript
Vibhor Grover
 
07 java collection
Abhishek Khune
 
Javascript arrays
Hassan Dar
 
enums
teach4uin
 
Introduction to Javascript
Amit Tyagi
 
Asynchronous JavaScript Programming with Callbacks & Promises
Hùng Nguyễn Huy
 
Typescript ppt
akhilsreyas
 
Introduction to web programming with JavaScript
T11 Sessions
 
what is functional component
manojbkalla
 
TypeScript
Saray Chak
 
Clean coding-practices
John Ferguson Smart Limited
 
Object Oriented Programming In JavaScript
Forziatech
 
Clean code
Arturo Herrero
 
Asynchronous JavaScript Programming
Haim Michael
 
Java Collection framework
ankitgarg_er
 
Promises, promises, and then observables
Stefan Charsley
 
TypeScript Introduction
Dmitry Sheiko
 
Introduction to gradle
NexThoughts Technologies
 

Similar to Let's discover React and Redux with TypeScript (20)

PDF
React Fundamentals - Jakarta JS, Apr 2016
Simon Sturmer
 
PDF
Introduction to ReactJS
Daine Mawer
 
PDF
React & Flux Workshop
Christian Lilley
 
PDF
How to Redux
Ted Pennings
 
PDF
React for Dummies
Mitch Chen
 
PDF
Materi Modern React Redux Power Point.pdf
exiabreak
 
PPTX
React js programming concept
Tariqul islam
 
PPTX
ProvJS: Six Months of ReactJS and Redux
Thom Nichols
 
PPTX
React redux
Ramy ElBasyouni
 
PDF
React Native +Redux + ES6 (Updated)
Chiew Carol
 
PDF
Reactивная тяга
Vitebsk Miniq
 
PDF
An Introduction to React -- FED Date -- IBM Design
Josh Black
 
PPTX
an Introduction to Redux
Amin Ashtiani
 
PDF
Reactive programming with RxJS - Taiwan
modernweb
 
PPTX
Redux Like Us
Heber Silva
 
PDF
React
Amitai Barnea
 
PDF
Purifying React (with annotations)
Robin Pokorny
 
PDF
Intro to react_v2
Feather Knee
 
PPTX
Introduction to react and redux
Cuong Ho
 
PDF
State of the state
Anton Korzunov
 
React Fundamentals - Jakarta JS, Apr 2016
Simon Sturmer
 
Introduction to ReactJS
Daine Mawer
 
React & Flux Workshop
Christian Lilley
 
How to Redux
Ted Pennings
 
React for Dummies
Mitch Chen
 
Materi Modern React Redux Power Point.pdf
exiabreak
 
React js programming concept
Tariqul islam
 
ProvJS: Six Months of ReactJS and Redux
Thom Nichols
 
React redux
Ramy ElBasyouni
 
React Native +Redux + ES6 (Updated)
Chiew Carol
 
Reactивная тяга
Vitebsk Miniq
 
An Introduction to React -- FED Date -- IBM Design
Josh Black
 
an Introduction to Redux
Amin Ashtiani
 
Reactive programming with RxJS - Taiwan
modernweb
 
Redux Like Us
Heber Silva
 
Purifying React (with annotations)
Robin Pokorny
 
Intro to react_v2
Feather Knee
 
Introduction to react and redux
Cuong Ho
 
State of the state
Anton Korzunov
 
Ad

Recently uploaded (20)

PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Ad

Let's discover React and Redux with TypeScript

  • 1. REACT AND REDUX LET'S DISCOVER… Mathieu Savy | @mrblackus
  • 2. LET'S DISCOVER REACT AND REDUX WHAT IS REACT? ▸ JS library to build User Interface (UI) ▸ Everything is Component ▸ Unidirectional data-flow (from parent to child) ▸ Virtual DOM ▸ NO MORE! (no controller, no router, no filter, etc.)
  • 4. LET'S DISCOVER REACT AND REDUX AppComponent
  • 5. LET'S DISCOVER REACT AND REDUX AppComponent SearchBarComponent
  • 6. LET'S DISCOVER REACT AND REDUX AppComponent SearchBarComponent SortBarComponent
  • 7. LET'S DISCOVER REACT AND REDUX AppComponent QuestionListComponent SearchBarComponent SortBarComponent
  • 8. LET'S DISCOVER REACT AND REDUX AppComponent QuestionListComponent QuestionComponent SearchBarComponent SortBarComponent
  • 9. LET'S DISCOVER REACT AND REDUX AppComponent QuestionListComponent QuestionComponent LabelComponent BadgeComponent SearchBarComponent SortBarComponent
  • 10. LET'S DISCOVER REACT AND REDUX COMPONENT
  • 11. LET'S DISCOVER REACT AND REDUX I'm pretty sure you can't do that. COMPONENT
  • 12. LET'S DISCOVER REACT AND REDUX Anyway, you shouldn't. Separation of concerns, MVC, bla bla bla… 🤓 COMPONENT
  • 13. LET'S DISCOVER REACT AND REDUX And that?! Anyway, you shouldn't. Separation of concerns, MVC, bla bla bla… COMPONENT
  • 14. LET'S DISCOVER REACT AND REDUX PROPERTIES ▸ Used to pass values from a parent component to a child component ▸ Like HTML attributes ▸ Immutable values (can't be used to pass data from child to parent) Will display : "Hey! Hello Mathieu!"
  • 15. LET'S DISCOVER REACT AND REDUX STATE ▸ Represent the internal state of a component ▸ State can be modified ▸ Must have an initial value ▸ Component is re-rendered if the state is modified
  • 16. LET'S DISCOVER REACT AND REDUX STATE
  • 17. LET'S DISCOVER REACT AND REDUX STATE And if I want to choose my initial state at runtime? 🤔
  • 18. LET'S DISCOVER REACT AND REDUX STATE Use props! 😃
  • 19. LET'S DISCOVER REACT AND REDUX LET'S TYPE CHECK OUR COUNTER ▸ We use TypeScript for type checking, so let's be sure our number is a number (who knows…) ▸ Remember the <any, any> part of React.Component?
  • 20. LET'S DISCOVER REACT AND REDUX LET'S TYPE CHECK OUR COUNTER Now, you can try to put something else than a number on our counter, it won't work
  • 21. LET'S DISCOVER REACT AND REDUX LET'S TYPE CHECK OUR COUNTER
  • 22. LET'S DISCOVER REACT AND REDUX LET'S TYPE CHECK OUR COUNTER 🕵
  • 23. LET'S DISCOVER REACT AND REDUX Type checking = peace of mind
  • 24. LET'S DISCOVER REACT AND REDUX DATA FLOW ▸ Data flows from parent to children components ▸ To pass values to a child, we use props ▸ There only is a one way data-binding
  • 26. LET'S DISCOVER REACT AND REDUX DATA FLOW 🤔 But now, I want to mark an item as "done" when I click on it. Props are immutable, so I can't do this.props.item.done = true; How can I warn the parent to mark my item as "done" ? ❌
  • 27. LET'S DISCOVER REACT AND REDUX DATA FLOW 🤔 But now, I want to mark an item as "done" when I click on it. Props are immutable, so I can't do this.props.item.done = true; How can I warn the parent to mark my item as "done" ? EVENTS Data flows down, events flow up
  • 28. LET'S DISCOVER REACT AND REDUX DATA FLOW 1.On TodoList component, define an action we want to execute when clicking on a TodoItem 2.Pass this action as a prop to every TodoItem 3.When click is triggered on TodoItem, call the action that was passed as prop
  • 29. LET'S DISCOVER REACT AND REDUX DATA FLOW 1.On TodoList component, define an action we want to execute when clicking on a TodoItem 2.Pass this action as a prop to every TodoItem 3.When click is triggered on TodoItem, call the action that was passed as prop DELEGATION
  • 31. Data
  • 33. LET'S DISCOVER REACT AND REDUX VIRTUAL DOM ▸ DOM manipulations are (very) slow ▸ Use of Virtual DOM allows us to re-render the DOM only when necessary ▸ Re-render only needed subtrees (diff between old state and new state) ▸ Allow server-side rendering ▸ => Better performance
  • 34. LET'S DISCOVER REACT AND REDUX VIRTUAL DOM Credit: http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
  • 35. LET'S DISCOVER REACT AND REDUX VIRTUAL DOM Credit: http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
  • 36. LET'S DISCOVER REACT AND REDUX OKAY, THAT SEEMS COOL. BUT WHY? ▸ UI became predictable and deterministic ▸ Predictable => easier to understand and test ▸ Components are reusable and more maintainable ▸ Use of virtual DOM allows very good performances
  • 37. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application?
  • 38. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application? Server response
  • 39. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application? Server response Cached data
  • 40. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application? Server response Cached data Temporary form data
  • 41. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application? Server response Cached dataUI state Temporary form data
  • 42. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application? Server response Cached dataUI state Temporary form data Loader during async tasks
  • 43. LET'S DISCOVER REACT AND REDUX Hey, it's okay for a todo-list. But what about a real application? Server response Cached dataUI state Temporary form data Loader during async tasks Error message
  • 44. LET'S DISCOVER REACT AND REDUX State management is going to be a nightmare!
  • 45. LET'S DISCOVER REACT AND REDUX State management is going to be a nightmare! 😈
  • 46. LET'S DISCOVER REACT AND REDUX Flux Relay Redux to the rescue
  • 47. LET'S DISCOVER REACT AND REDUX WHAT IS REDUX? "Redux is a predictable state container for JavaScript apps." http://redux.js.org
  • 48. LET'S DISCOVER REACT AND REDUX WHAT IS REDUX? State container? There is one unique state container called store. It contains your application state. Same store = same application render
  • 49. LET'S DISCOVER REACT AND REDUX WHAT IS REDUX? Predictable? State is immutable We can get a new state by dispatching actions
  • 50. LET'S DISCOVER REACT AND REDUX WHAT IS REDUX? Predictable? State is immutable We can get a new state by dispatching actions "Mark item 3 as read" {
 type: "MARK_READ",
 itemId: 3
 } =>
  • 51. LET'S DISCOVER REACT AND REDUX WHAT IS REDUX? Predictable? Actions are consumed by reducers Reducers take an action, the actual state and return a new state function reducer(state, action) => newState
  • 52. LET'S DISCOVER REACT AND REDUX WHAT IS REDUX? Predictable? Reducers are pure functions. Same parameters = same result. Always.
  • 53. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? Let's build a counter (yeah, again)! ▸ We have two actions: increment and decrement ▸ If another (or an unknown) action is passed, do nothing (i.e. return the current state)
  • 54. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? The reducer
  • 55. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? The reducer 🤔 Hum, on the first call, state will be undefined, isn't it?
  • 56. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? The reducer ES6 default values to the rescue!
 This is the way to define our app initial state.
  • 57. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? The store We create the store by passing our reducer to Redux createStore() function
  • 58. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? And we are done! Yep, Redux is that simple. If you got this, you understood all the basics of Redux.
  • 59. LET'S DISCOVER REACT AND REDUX A LITTLE EXAMPLE? Hey, don't trick me!
 There's not a single line of React in your example. 😐
  • 60. LET'S DISCOVER REACT AND REDUX REDUX × REACT ▸ Use react-redux package ▸ Relies on presentational and container components
  • 61. LET'S DISCOVER REACT AND REDUX REDUX × REACT: PRESENTATIO-WHAT? Credit: http://redux.js.org/docs/basics/UsageWithReact.html
  • 62. LET'S DISCOVER REACT AND REDUX REDUX × REACT: PRESENTATIONAL COMPONENTS ▸ Data from props ▸ Invoke callbacks (from props) to change data — Delegation ▸ Rarely have a state (in such case, just UI state, no data)
  • 63. LET'S DISCOVER REACT AND REDUX REDUX × REACT: CONTAINER COMPONENTS ▸ Are generated (by react-redux) to work with a presentational component ▸ Generated from two functions that describes how to: ▸ map the state to child component props ▸ map dispatch calls to child components props ▸ Need Redux store passed as property
  • 64. LET'S DISCOVER REACT AND REDUX REDUX × REACT: CONTAINER COMPONENTS
  • 65. LET'S DISCOVER REACT AND REDUX REDUX × REACT: CONTAINER COMPONENTS Presentational Component
  • 66. LET'S DISCOVER REACT AND REDUX REDUX × REACT: CONTAINER COMPONENTS Presentational Component Counter component will have count and onIncrement properties
  • 67. LET'S DISCOVER REACT AND REDUX REDUX × REACT: CONTAINER COMPONENTS Presentational Component Counter component will have count and onIncrement properties Redux Store
  • 68. LET'S DISCOVER REACT AND REDUX REDUX × REACT: CONTAINER COMPONENTS Passing store to every container components will rapidly become impossible to handle properly. That's why react-redux exposes a Provider component that handles it for us.
  • 69. LET'S DISCOVER REACT AND REDUX A LITTLE MORE: ASYNC Once again, that's cool for a todo-list, but what about async calls with Redux? ▸ See redux-thunk middleware ▸ Notion of async action, that dispatches actions (request start, request end, etc.) More: http://redux.js.org/docs/advanced/AsyncActions.html
  • 70. LET'S DISCOVER REACT AND REDUX RESOURCES ▸ React doc: https://facebook.github.io/react/ ▸ Holy Redux Bible: http://redux.js.org/index.html