SlideShare a Scribd company logo
Flux Architecture
Jakub Kocikowski
About me
● Software engineer for over 5 years
● Fan of simple solutions to complex
problems
● I like bridges
Agenda
1. Theory Part 1: Flux and Redux
2. Theory Part 2: Context
3. Practical Considerations
1/3
Theory - Flux and Redux
MVC
The problem with MVC
Given multiple interconnected components
When an event arrives at the system
Then it causes cascading updates
The problem with MVC
On the sufficiently large code base:
1. It is hard to reason about data flow
2. New modules increase complexity exponentially
3. The code base is fragile
The solution
(state, action) => newState
Flux Architecture
View
● Listens to changes in the stores
● Dispatches actions to dispatcher
Store
● Encapsulating a whole domain
● Contains state
● Contains logic to update the state based on actions
● Notifies views about data changes
Dispatcher
● Single dispatcher per application
● Enforces one-directional synchronous data flow
● Receives actions and dispatches them to stores
● Allows you to specify dependencies between the stores
Action
● A simple object
● Has a type and some data
Action
● A simple object
● Has a type and some data
Action creators
● Factory for the action
● Makes sure the action is
formatted correctly
Flux Architecture
LET’S
TALK
ABOUT
RE-DUCKS
Redux
Flux vs Redux
Multiple stores
Dispatcher
Store updates state
Can mutate the data
Change events
One store
No Dispatcher
Reducer updates state
Immutable
Always rerender
The Redux way
Reducers
● Pure function
● Takes current state and an action
● Returns a new state
dispatch()
● A simple function
● What it does:
1. Calls reducer with current state and action
2. Replaces current state
3. Notifies listeners
Redux
Selectors
● Pure function
● Takes the state as an argument
● Returns computed value
Middleware
Redux
benefits
● Time travel
● Snapshots
● Easy hydration
● Shallow comparison
2/3
Theory II - Context
CQRS
Command Query Responsibility
Segregation [...] is the notion that
you can use a different model to
update information than the model
you use to read information
- Martin Folwer
Image: https://www.martinfowler.com/bliki/CQRS.html
Flux Architecture
Event Sourcing
elm
Elm
● Functional language for frontend
● All the side effects are managed by the runtime
● Strict, Structural typing
● Compiles to JavaScript
Reactive
programming
Reactive programming
● Declarative paradigm were time is a first-class citizen
● Two important types
○ signals - continuous function over time
○ streams - collection of discrete values over time
● We build the pipes through which we start pumping events
● Great to handle async and concurrency
Source: https://github.com/Reactive-Extensions/RxJS
How does it relate to Flux?
● You can implement it using a stream
● You can use a middleware to bridge RxJS and Redux
3/3
Practical considerations
THE DOWNSIDES
The downsides
1. Boilerplate and indirection
2. Requires immutability
3. Requires all important state to live in one place
4. Stores only serializable data
5. Performance
6. Potentially hard to learn
7. Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Nested state may cause independent views to rerender when the state changes
Are there any alternatives? A word about Mobx
● more automation, less boilerplate
● less explicit
● no time travel
● simpler
● mutable
● observable
● multiple stores
● more OOP
What about setState?
● Not an antipattern
● Probably you should start from that
● If your state maps well to component structure
● Maybe Context
So when to use Flux?
● Dependencies between data and complex update logic
● Distant leaf components need access to state
● Your data and it’s lifecycle matches event-based architecture
● You want to use benefits like easy hydration
I have this data I need to store
somewhere…
The rule of thumb is:
do whatever is less awkward.
Dan Abramov
I have this data I need to store somewhere…
● What is the convention in your team?
● Do I want to use specific Redux features?
Like cache the data between reloads in localStorage?
I have this data I need to store somewhere…
1. Why is it changing?
2. Is it causing other values to change?
I have this data I need to store somewhere…
● What components need to know about the data?
Sources / Learn more
Flux and Redux
● Facebook Flux documentation https://facebook.github.io/flux/
● Redux documentation https://redux.js.org/
● From MVC to Flux https://www.youtube.com/watch?v=p8tqhf5qKOI
● Differences between Flux and Redux https://medium.com/edge-coders/the-difference-between-flux-and-redux-71d31b118c1
● Interview with the creator of React https://devchat.tv/js-jabber/179-jsj-redux-and-react-with-dan-abramov/
● Type Of Web on Redux, Flux, CQRS, rules of thumb https://typeofweb.com/2018/03/29/flux-i-redux/
● When to use local state and when to use Redux https://typeofweb.com/2018/05/16/kiedy-uzywac-state-a-kiedy-redux/
● Redux vs Mobx https://www.youtube.com/watch?v=76FRrbY18Bs
● You may not need Redux https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367
● Downsides of Redux https://github.com/reduxjs/redux/issues/1385#issuecomment-182632703
● How to choose between Redux's store and React's state? - rule of thumb form Dan Abramov https://github.com/reduxjs/redux/issues/1287
● Comparison of some implementations of Flux https://github.com/voronianski/flux-comparison#ready
Sources / Learn more
Elm
● Elm documentation https://guide.elm-lang.org/architecture/
● Podcast on Elm https://softwareengineeringdaily.com/2015/11/03/elm-with-richard-feldman-and-srinivas-rao/
● Tutorial for sign up form in Elm https://blog.noredink.com/post/129641182738/building-a-live-validated-signup-form-in-elm
● Motivation for creating Elm https://venturebeat.com/2013/07/26/why-i-designed-a-front-end-programming-language-from-scratch/
● Talk on Elm https://www.youtube.com/watch?v=EDp6UmaA9CM
Others
● Reactive Javascript https://softwareengineeringdaily.com/2016/10/25/reactive-javascript-with-ben-lesh/
● ReactiveX Documentation http://reactivex.io/
● Reactive Programming at Netflix https://www.youtube.com/watch?v=AslncyG8whg
● CQRS https://www.martinfowler.com/bliki/CQRS.html
● Event Sourcing https://martinfowler.com/eaaDev/EventSourcing.html
Images used
1. CQRS diagram - https://www.martinfowler.com/bliki/CQRS.html
2. Duck gifs - https://giphy.com
3. Social media icons - https://flaticon.com
4. Martin Fowler photo https://en.wikipedia.org/wiki/Martin_Fowler_(software_engineer)#/media/File:Webysther_20150414193208_-_Martin_Fowler.jpg
5. Dan Abramov photo https://cdn-images-1.medium.com/max/1200/1*Vko_9kRNbjQGCqyBM9OnVw.jpeg
6. Diagram of MVC gone wrong from slide 8 - https://youtu.be/nYkdrAPrdcw
7. Rest of the diagrams are by me, created in http://draw.io
Thank you
Jakub Kocikowski
Software Engineer
Contact me:
@kocikowski

More Related Content

Similar to Flux architecture and Redux - theory, context and practice

FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and PatternsFRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
Leapfrog Technology Inc.
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
Matthias Noback
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
Ramith Jayasinghe
 
Introduction to Redux.pptx
Introduction to Redux.pptxIntroduction to Redux.pptx
Introduction to Redux.pptx
MohammadImran322154
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
Ramith Jayasinghe
 
Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"
LogeekNightUkraine
 
Robust web apps with React.js
Robust web apps with React.jsRobust web apps with React.js
Robust web apps with React.js
Max Klymyshyn
 
Lagom : Reactive microservice framework
Lagom : Reactive microservice frameworkLagom : Reactive microservice framework
Lagom : Reactive microservice framework
Fabrice Sznajderman
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenches
Johannes Rudolph
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - Linagora
LINAGORA
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
Hùng Nguyễn Huy
 
Daniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého SchizmaDaniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého Schizma
Develcz
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
Piyush Katariya
 
Building Reactive webapp with React/Flux
Building Reactive webapp with React/FluxBuilding Reactive webapp with React/Flux
Building Reactive webapp with React/Flux
Keuller Magalhães
 
React - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesReact - The JavaScript Library for User Interfaces
React - The JavaScript Library for User Interfaces
Jumping Bean
 
Bots on guard of sdlc
Bots on guard of sdlcBots on guard of sdlc
Bots on guard of sdlc
Alexey Tokar
 
Tips and tricks for developing streaming and table connectors - Eron Wright,...
Tips and tricks for developing streaming and table connectors  - Eron Wright,...Tips and tricks for developing streaming and table connectors  - Eron Wright,...
Tips and tricks for developing streaming and table connectors - Eron Wright,...
Flink Forward
 
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Flink Forward
 
Flink Connector Development Tips & Tricks
Flink Connector Development Tips & TricksFlink Connector Development Tips & Tricks
Flink Connector Development Tips & Tricks
Eron Wright
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
Jeffrey Sanchez
 

Similar to Flux architecture and Redux - theory, context and practice (20)

FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and PatternsFRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
FRU Kathmandu: Adopting with Change Frontend Architecture and Patterns
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Introduction to Redux.pptx
Introduction to Redux.pptxIntroduction to Redux.pptx
Introduction to Redux.pptx
 
Concurrency - Why it's hard ?
Concurrency - Why it's hard ?Concurrency - Why it's hard ?
Concurrency - Why it's hard ?
 
Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"Andrii Sliusar "Module Architecture of React-Redux Applications"
Andrii Sliusar "Module Architecture of React-Redux Applications"
 
Robust web apps with React.js
Robust web apps with React.jsRobust web apps with React.js
Robust web apps with React.js
 
Lagom : Reactive microservice framework
Lagom : Reactive microservice frameworkLagom : Reactive microservice framework
Lagom : Reactive microservice framework
 
Angular2 - A story from the trenches
Angular2 - A story from the trenchesAngular2 - A story from the trenches
Angular2 - A story from the trenches
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - Linagora
 
Redux in Angular 2+
Redux in Angular 2+Redux in Angular 2+
Redux in Angular 2+
 
Daniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého SchizmaDaniel Steigerwald - Este.js - konec velkého Schizma
Daniel Steigerwald - Este.js - konec velkého Schizma
 
JavaScript for Enterprise Applications
JavaScript for Enterprise ApplicationsJavaScript for Enterprise Applications
JavaScript for Enterprise Applications
 
Building Reactive webapp with React/Flux
Building Reactive webapp with React/FluxBuilding Reactive webapp with React/Flux
Building Reactive webapp with React/Flux
 
React - The JavaScript Library for User Interfaces
React - The JavaScript Library for User InterfacesReact - The JavaScript Library for User Interfaces
React - The JavaScript Library for User Interfaces
 
Bots on guard of sdlc
Bots on guard of sdlcBots on guard of sdlc
Bots on guard of sdlc
 
Tips and tricks for developing streaming and table connectors - Eron Wright,...
Tips and tricks for developing streaming and table connectors  - Eron Wright,...Tips and tricks for developing streaming and table connectors  - Eron Wright,...
Tips and tricks for developing streaming and table connectors - Eron Wright,...
 
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
Tips and Tricks for Developing Streaming and table Connectors - Wron Wright, ...
 
Flink Connector Development Tips & Tricks
Flink Connector Development Tips & TricksFlink Connector Development Tips & Tricks
Flink Connector Development Tips & Tricks
 
The Road To Redux
The Road To ReduxThe Road To Redux
The Road To Redux
 

Recently uploaded

Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 

Recently uploaded (20)

Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 

Flux architecture and Redux - theory, context and practice

  • 2. About me ● Software engineer for over 5 years ● Fan of simple solutions to complex problems ● I like bridges
  • 3.
  • 4.
  • 5. Agenda 1. Theory Part 1: Flux and Redux 2. Theory Part 2: Context 3. Practical Considerations
  • 6. 1/3 Theory - Flux and Redux
  • 7. MVC
  • 8.
  • 9. The problem with MVC Given multiple interconnected components When an event arrives at the system Then it causes cascading updates
  • 10. The problem with MVC On the sufficiently large code base: 1. It is hard to reason about data flow 2. New modules increase complexity exponentially 3. The code base is fragile
  • 13. View ● Listens to changes in the stores ● Dispatches actions to dispatcher
  • 14. Store ● Encapsulating a whole domain ● Contains state ● Contains logic to update the state based on actions ● Notifies views about data changes
  • 15. Dispatcher ● Single dispatcher per application ● Enforces one-directional synchronous data flow ● Receives actions and dispatches them to stores ● Allows you to specify dependencies between the stores
  • 16. Action ● A simple object ● Has a type and some data
  • 17. Action ● A simple object ● Has a type and some data
  • 18. Action creators ● Factory for the action ● Makes sure the action is formatted correctly
  • 21. Redux
  • 22. Flux vs Redux Multiple stores Dispatcher Store updates state Can mutate the data Change events One store No Dispatcher Reducer updates state Immutable Always rerender
  • 24. Reducers ● Pure function ● Takes current state and an action ● Returns a new state
  • 25. dispatch() ● A simple function ● What it does: 1. Calls reducer with current state and action 2. Replaces current state 3. Notifies listeners
  • 26. Redux
  • 27. Selectors ● Pure function ● Takes the state as an argument ● Returns computed value
  • 29. Redux benefits ● Time travel ● Snapshots ● Easy hydration ● Shallow comparison
  • 30. 2/3 Theory II - Context
  • 31. CQRS Command Query Responsibility Segregation [...] is the notion that you can use a different model to update information than the model you use to read information - Martin Folwer Image: https://www.martinfowler.com/bliki/CQRS.html
  • 34. elm
  • 35. Elm ● Functional language for frontend ● All the side effects are managed by the runtime ● Strict, Structural typing ● Compiles to JavaScript
  • 36.
  • 38. Reactive programming ● Declarative paradigm were time is a first-class citizen ● Two important types ○ signals - continuous function over time ○ streams - collection of discrete values over time ● We build the pipes through which we start pumping events ● Great to handle async and concurrency
  • 39.
  • 41. How does it relate to Flux? ● You can implement it using a stream ● You can use a middleware to bridge RxJS and Redux
  • 44. The downsides 1. Boilerplate and indirection 2. Requires immutability 3. Requires all important state to live in one place 4. Stores only serializable data 5. Performance 6. Potentially hard to learn 7. Nested state may cause independent views to rerender when the state changes
  • 45. Nested state may cause independent views to rerender when the state changes
  • 46. Nested state may cause independent views to rerender when the state changes
  • 47. Nested state may cause independent views to rerender when the state changes
  • 48. Nested state may cause independent views to rerender when the state changes
  • 49. Are there any alternatives? A word about Mobx ● more automation, less boilerplate ● less explicit ● no time travel ● simpler ● mutable ● observable ● multiple stores ● more OOP
  • 50. What about setState? ● Not an antipattern ● Probably you should start from that ● If your state maps well to component structure ● Maybe Context
  • 51. So when to use Flux? ● Dependencies between data and complex update logic ● Distant leaf components need access to state ● Your data and it’s lifecycle matches event-based architecture ● You want to use benefits like easy hydration
  • 52. I have this data I need to store somewhere…
  • 53. The rule of thumb is: do whatever is less awkward. Dan Abramov
  • 54. I have this data I need to store somewhere… ● What is the convention in your team? ● Do I want to use specific Redux features? Like cache the data between reloads in localStorage?
  • 55. I have this data I need to store somewhere… 1. Why is it changing? 2. Is it causing other values to change?
  • 56. I have this data I need to store somewhere… ● What components need to know about the data?
  • 57.
  • 58. Sources / Learn more Flux and Redux ● Facebook Flux documentation https://facebook.github.io/flux/ ● Redux documentation https://redux.js.org/ ● From MVC to Flux https://www.youtube.com/watch?v=p8tqhf5qKOI ● Differences between Flux and Redux https://medium.com/edge-coders/the-difference-between-flux-and-redux-71d31b118c1 ● Interview with the creator of React https://devchat.tv/js-jabber/179-jsj-redux-and-react-with-dan-abramov/ ● Type Of Web on Redux, Flux, CQRS, rules of thumb https://typeofweb.com/2018/03/29/flux-i-redux/ ● When to use local state and when to use Redux https://typeofweb.com/2018/05/16/kiedy-uzywac-state-a-kiedy-redux/ ● Redux vs Mobx https://www.youtube.com/watch?v=76FRrbY18Bs ● You may not need Redux https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367 ● Downsides of Redux https://github.com/reduxjs/redux/issues/1385#issuecomment-182632703 ● How to choose between Redux's store and React's state? - rule of thumb form Dan Abramov https://github.com/reduxjs/redux/issues/1287 ● Comparison of some implementations of Flux https://github.com/voronianski/flux-comparison#ready
  • 59. Sources / Learn more Elm ● Elm documentation https://guide.elm-lang.org/architecture/ ● Podcast on Elm https://softwareengineeringdaily.com/2015/11/03/elm-with-richard-feldman-and-srinivas-rao/ ● Tutorial for sign up form in Elm https://blog.noredink.com/post/129641182738/building-a-live-validated-signup-form-in-elm ● Motivation for creating Elm https://venturebeat.com/2013/07/26/why-i-designed-a-front-end-programming-language-from-scratch/ ● Talk on Elm https://www.youtube.com/watch?v=EDp6UmaA9CM Others ● Reactive Javascript https://softwareengineeringdaily.com/2016/10/25/reactive-javascript-with-ben-lesh/ ● ReactiveX Documentation http://reactivex.io/ ● Reactive Programming at Netflix https://www.youtube.com/watch?v=AslncyG8whg ● CQRS https://www.martinfowler.com/bliki/CQRS.html ● Event Sourcing https://martinfowler.com/eaaDev/EventSourcing.html
  • 60. Images used 1. CQRS diagram - https://www.martinfowler.com/bliki/CQRS.html 2. Duck gifs - https://giphy.com 3. Social media icons - https://flaticon.com 4. Martin Fowler photo https://en.wikipedia.org/wiki/Martin_Fowler_(software_engineer)#/media/File:Webysther_20150414193208_-_Martin_Fowler.jpg 5. Dan Abramov photo https://cdn-images-1.medium.com/max/1200/1*Vko_9kRNbjQGCqyBM9OnVw.jpeg 6. Diagram of MVC gone wrong from slide 8 - https://youtu.be/nYkdrAPrdcw 7. Rest of the diagrams are by me, created in http://draw.io
  • 61. Thank you Jakub Kocikowski Software Engineer Contact me: @kocikowski