SlideShare a Scribd company logo
1 of 63
Download to read offline
Dumb and smart
components & redux
Brecht Billiet
About me
Brecht Billiet
Frontend Architect - Coach - Trainer
http://brecht.io @brechtbilliet
Who still uses ng-
controller and ng-
include?
Doesn’t that bother you?
● What HTML belongs to what JS?
● Where do the templates live?
● Absolute template-paths are not maintainable
● What logic belongs in a controller or directive?
● This template should contain... oh wait, there is an ng-include there.
● How do you pass data to your controllers?
Think in components...
● 1 piece HTML + 1 piece LOGIC = COMPONENT
● You know it as: element directives
● Custom DOM tags
● A component has a single responsability
● We could use webcomponents + shadow DOM
● Sandboxed work
● Note: angular 1.5 has new component syntax
What should you
refactor to
components?
Everything is a component
● Your application is a tree of components
● Your pages are components
● Even your application is a component
application
Html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
application
Html + js
+ css
fooPage
html + js
+ css
html + js
+ css
barPage
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
html + js
+ css
Routingconfig:
template: ‘<foo-page/>’
Routingconfig:
template: ‘<bar-page/>’
Easier to debug
Other advantages
● No scope soup
● Nice hierarchy view
● Single responsability
● No more absolute paths
● Easier to reason about
● Easier to refactor
● Easier to upgrade to ng2
What about attribute-directives?
● Let’s call them behaviors from now
● They add some logic to existing or custom components
● Separate them from your components folder
DEMO
http://winecellar.surge.sh
http://winecellar.surge.sh: components
Any rules of thumb?
Keep them small...
Keep them dumb where possible...
Dumb component Smart component (container)
Doesn’t know about the
application
Knows about the application
Doesn’t do data fetching Does data fetching
Main purpose is visualization Interacts the application
Reusable Not reusable
Only communicates with its direct
parent
Passes state and data to its
children
Doesn’t do state management Does state management
http://winecellar.surge.sh: Smart
components
http://winecellar.surge.sh: Dumb
components
State management
● State is very hard to manage in SPA’s
● State can be
○ data: results from REST calls
○ “sidebar is collapsed” or “spinner is active”
○ Which tab is currently active
State management
● 3 types of state
○ Data state
○ Application state
○ Inner state (e.g value of textbox) We don’t have to persist nor manage that
Why is it so hard to manage?
● 1-way databinding and 2-way databinding
● javascript references that are being broken and sometimes kept
● We can mutate the data in a million different ways
● No structure
● Data flows in different directions
application
state &
logic
state &
logic
state &
logic
state &
logic
state &
logic
state &
logic
state &
logic
state &
logic
state &
logic
state &
logic
Without flux
1
2
5 6
3
7 8
4
9
10 11
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
I have to
subscribe
before 8
sends a
message
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
I have to
subscribe
before 8
sends a
message
I’m sending
an event
upwards
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
I have to
subscribe
before 8
sends a
message
I’m sending
an event
upwards
I’m sending
events
downwards
Without flux
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
I have to
subscribe
before 8
sends a
message
I’m sending
an event
upwards
Without flux
I’m sending
events
downwards
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
I have to
subscribe
before 8
sends a
message
I’m sending
an event
upwards
I get my data
from model x
and I can
update it
here
I get my data
from model z
Without flux
I’m sending
events
downwards
I get my data
from model y
1
2
5 6
3
7 8
4
9
10 11
I changed
So did I
My changes
should
reflect 6 and
4
I have to
subscribe
before 8
sends a
message
I’m sending
an event
upwards
I get my data
from model x
and I can
update it
here
I get my data
from model z
Without flux
My data is
passed by
twoway
binding
I’m sending
events
downwards
I get my data
from model y
My data is
passed by
oneway
binding
My data is
passed by
twoway
binding
Where do those states/events come from?
Who updated my state??
Who updated/notified which component?
Flux helps with that
● Brings structure in data flow
● Not a framework but an architecture by facebook
● Unidirectional dataflow, oneway databinding
● Data flows from the top down
● Easier to reason about state
● Action
○ Has an action type and a payload
● Dispatcher
○ Dispatches the actions to the store
● Store
○ Contains state
● View
Components
● The unidirectional dataflow makes it easy to reason about
I use Redux!
Redux is based on
flux, but it’s easier
Redux
● Written by Dan Abramov (Facebook)
● Easier to use and less bloat
● Only has one store!!
● Completely Immutable data structure
● Uses reducers (pure functions)
● Flux approves
1
2
5 6
3
7 8
4
9
10 11
STORE
1
2
5 6
3
7 8
4
9
10 11
Me too
Me too
Me too
Me too
STORE
I only send
actions
upwards to the
store
I’m the single-
source-of-truth
in your
application!! ;-)
1
2
5 6
3
7 8
4
9
10 11
Me too
Me too
I’m dumb I’m dumb
I’m dumb I’m dumb
I’m dumb
I’m dumb
STORE
Me too
I only send
actions
upwards to the
store Me too
I’m the single-
source-of-truth
in your
application!! ;-)
Dumb components only
notify their direct parents!
No state management
needed here :-)
What’s different?
● We have one single source of truth: the store
● We don’t update state ourselves anymore, the store handles all the state of
our application
● State is immutable
● Actions are always sent upwards
What’s different?
● Dumb components don’t do state management
● State is being updated by pure functions (reducers)
● When the store is updated it notifies all the smart components
Application state
Actions
● Actions have a type and a payload
● Keep the types together, gives clear overview
Actions
Reducers
● Pure functions
● reducer(state: MyState, action: Action): MyState
● they update the state
● Completely immutable
Reducers
Example in container
Middleware
● Easy logging, debugging
● Reproducible action state (pushable to server)
● Timetraveling with redux-devtools (requires react to be bundled in the
angular project)
Check it out
● egghead.io course (Free)
○ https://egghead.io/series/getting-started-with-redux
Special thanks to
● Kwinten Pisman (@kwintenp)
● Jurgen Van de Moere (@jvandemo)
Questions?
One more thing
Angular2, rxjs, redux,
@ngrx/store, Typescript,
webpack
http://workshop.brecht.io @brechtbilliet
Workshop
June 2016
Thank you!

More Related Content

What's hot

What's hot (19)

Let Codenarc check if you write good Groovy code
Let Codenarc check if you write good Groovy codeLet Codenarc check if you write good Groovy code
Let Codenarc check if you write good Groovy code
 
Html 5 tutorial - By Bally Chohan
Html 5 tutorial - By Bally ChohanHtml 5 tutorial - By Bally Chohan
Html 5 tutorial - By Bally Chohan
 
TechSEO Boost 2021 - Rendering Strategies: Measuring the Devil’s Details in C...
TechSEO Boost 2021 - Rendering Strategies: Measuring the Devil’s Details in C...TechSEO Boost 2021 - Rendering Strategies: Measuring the Devil’s Details in C...
TechSEO Boost 2021 - Rendering Strategies: Measuring the Devil’s Details in C...
 
Write Once, Run Everywhere
Write Once, Run EverywhereWrite Once, Run Everywhere
Write Once, Run Everywhere
 
Software Testing for SEO
Software Testing for SEOSoftware Testing for SEO
Software Testing for SEO
 
Nitro for your Grails App: How to improve performance!! Greach' 18
Nitro for your Grails App: How to improve performance!!  Greach' 18Nitro for your Grails App: How to improve performance!!  Greach' 18
Nitro for your Grails App: How to improve performance!! Greach' 18
 
Accessibility Report for training website
Accessibility Report for training websiteAccessibility Report for training website
Accessibility Report for training website
 
HTML5 with examples
HTML5 with examplesHTML5 with examples
HTML5 with examples
 
Headless SEO: Optimising Next Gen Sites | brightonSEO 2021
Headless SEO: Optimising Next Gen Sites | brightonSEO 2021Headless SEO: Optimising Next Gen Sites | brightonSEO 2021
Headless SEO: Optimising Next Gen Sites | brightonSEO 2021
 
Behaviour Driven Development
Behaviour Driven DevelopmentBehaviour Driven Development
Behaviour Driven Development
 
What I learned teaching programming to 150 beginners
What I learned teaching programming to 150 beginnersWhat I learned teaching programming to 150 beginners
What I learned teaching programming to 150 beginners
 
SEO Checklist for Google Mobile First Index
SEO Checklist for Google Mobile First IndexSEO Checklist for Google Mobile First Index
SEO Checklist for Google Mobile First Index
 
2 Seconds is the New Slow - Chris Simmance - under2
2 Seconds is the New Slow -  Chris Simmance - under22 Seconds is the New Slow -  Chris Simmance - under2
2 Seconds is the New Slow - Chris Simmance - under2
 
Rich HTML5 Web Apps: Typesafe Edition
Rich HTML5 Web Apps: Typesafe EditionRich HTML5 Web Apps: Typesafe Edition
Rich HTML5 Web Apps: Typesafe Edition
 
Technical SEO "Overoptimization"
Technical SEO "Overoptimization"Technical SEO "Overoptimization"
Technical SEO "Overoptimization"
 
The New Renaissance of JavaScript
The New Renaissance of JavaScriptThe New Renaissance of JavaScript
The New Renaissance of JavaScript
 
React native - What, Why, How?
React native - What, Why, How?React native - What, Why, How?
React native - What, Why, How?
 
TechSEO Boost 2018: The Statelessness of Technical SEO
TechSEO Boost 2018: The Statelessness of Technical SEOTechSEO Boost 2018: The Statelessness of Technical SEO
TechSEO Boost 2018: The Statelessness of Technical SEO
 
The Great Migration: Moving Your SharePoint | Fpwebinar
The Great Migration: Moving Your SharePoint | Fpwebinar The Great Migration: Moving Your SharePoint | Fpwebinar
The Great Migration: Moving Your SharePoint | Fpwebinar
 

Viewers also liked

Viewers also liked (17)

Redux in Angular2 for jsbe
Redux in Angular2 for jsbeRedux in Angular2 for jsbe
Redux in Angular2 for jsbe
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Into the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional ProgrammingInto the Land of lambda, One Programmer's Journey Into Functional Programming
Into the Land of lambda, One Programmer's Journey Into Functional Programming
 
React. Flux. Redux. by Andrey Kolodnitskiy
React. Flux. Redux. by Andrey KolodnitskiyReact. Flux. Redux. by Andrey Kolodnitskiy
React. Flux. Redux. by Andrey Kolodnitskiy
 
Understanding Redux — Ilya Gelman
Understanding Redux — Ilya GelmanUnderstanding Redux — Ilya Gelman
Understanding Redux — Ilya Gelman
 
Application architecture doesn't have to suck
Application architecture doesn't have to suckApplication architecture doesn't have to suck
Application architecture doesn't have to suck
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creators
 
Side Effects: Uma Saga até o React
Side Effects: Uma Saga até o ReactSide Effects: Uma Saga até o React
Side Effects: Uma Saga até o React
 
'Did He Really Say That?" effective component communication
'Did He Really Say That?" effective component communication'Did He Really Say That?" effective component communication
'Did He Really Say That?" effective component communication
 
React. Flux. Redux
React. Flux. ReduxReact. Flux. Redux
React. Flux. Redux
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga begins
 
Angular 2 Component Communication - Talk by Rob McDiarmid
Angular 2 Component Communication - Talk by Rob McDiarmidAngular 2 Component Communication - Talk by Rob McDiarmid
Angular 2 Component Communication - Talk by Rob McDiarmid
 
Simple made easy
Simple made easySimple made easy
Simple made easy
 
RxJS + Redux + React = Amazing
RxJS + Redux + React = AmazingRxJS + Redux + React = Amazing
RxJS + Redux + React = Amazing
 
Are we there yet?
Are we there yet?Are we there yet?
Are we there yet?
 
Asyc flow control with javascript generators - redux-saga
Asyc flow control with javascript generators - redux-sagaAsyc flow control with javascript generators - redux-saga
Asyc flow control with javascript generators - redux-saga
 
UX Jam x UX Sketch 2017 HD
UX Jam x UX Sketch 2017 HDUX Jam x UX Sketch 2017 HD
UX Jam x UX Sketch 2017 HD
 

Similar to Dumb and smart components + redux (1)

orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...
orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...
orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...
drakselva2011
 

Similar to Dumb and smart components + redux (1) (20)

Talkin bout Flow - Meighan Brodkey WIT Devs
Talkin bout Flow - Meighan Brodkey WIT Devs Talkin bout Flow - Meighan Brodkey WIT Devs
Talkin bout Flow - Meighan Brodkey WIT Devs
 
Denver Salesforce Developer User Group dec 2016 lightning components
Denver Salesforce Developer User Group dec 2016 lightning componentsDenver Salesforce Developer User Group dec 2016 lightning components
Denver Salesforce Developer User Group dec 2016 lightning components
 
Best Practices in Automating Business Processes
Best Practices in Automating Business ProcessesBest Practices in Automating Business Processes
Best Practices in Automating Business Processes
 
Turning the web stack upside down rethinking how data flows through systems
Turning the web stack upside down  rethinking how data flows through systemsTurning the web stack upside down  rethinking how data flows through systems
Turning the web stack upside down rethinking how data flows through systems
 
Frappe Open Day January 2019
Frappe Open Day January 2019Frappe Open Day January 2019
Frappe Open Day January 2019
 
Angular 2 - An Introduction
Angular 2 - An IntroductionAngular 2 - An Introduction
Angular 2 - An Introduction
 
Python for Data Logistics
Python for Data LogisticsPython for Data Logistics
Python for Data Logistics
 
Destination Documentation: How Not to Get Lost in Your Org
Destination Documentation: How Not to Get Lost in Your OrgDestination Documentation: How Not to Get Lost in Your Org
Destination Documentation: How Not to Get Lost in Your Org
 
orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...
orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...
orphanemgensysmtelsjfdinadlfhlajfdljflajdfkljalkdjflkjlajfeohrlejkldjlkjglajf...
 
My first year with event sourcing-nijmegen
My first year with event sourcing-nijmegenMy first year with event sourcing-nijmegen
My first year with event sourcing-nijmegen
 
KScope14 Understanding the Zombies that lurk within your system
KScope14 Understanding the Zombies that lurk within your systemKScope14 Understanding the Zombies that lurk within your system
KScope14 Understanding the Zombies that lurk within your system
 
Sea of Data
Sea of DataSea of Data
Sea of Data
 
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
 
Data Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJSData Sharing Between Child and Parent Components in AngularJS
Data Sharing Between Child and Parent Components in AngularJS
 
Double Loop: TDD & BDD Done Right!
Double Loop: TDD & BDD Done Right!Double Loop: TDD & BDD Done Right!
Double Loop: TDD & BDD Done Right!
 
React learning in the hard way
React   learning in the hard wayReact   learning in the hard way
React learning in the hard way
 
Building a site for people with big imaginations
Building a site for people with big imaginationsBuilding a site for people with big imaginations
Building a site for people with big imaginations
 
WEBINAR: Proven Patterns for Loading Test Data for Managed Package Testing
WEBINAR: Proven Patterns for Loading Test Data for Managed Package TestingWEBINAR: Proven Patterns for Loading Test Data for Managed Package Testing
WEBINAR: Proven Patterns for Loading Test Data for Managed Package Testing
 
Wiring the IoT for modern manufacturing
Wiring the IoT for modern manufacturingWiring the IoT for modern manufacturing
Wiring the IoT for modern manufacturing
 
Reduxing UI: Borrowing the Best of Web to Make Android Better
Reduxing UI: Borrowing the Best of Web to Make Android BetterReduxing UI: Borrowing the Best of Web to Make Android Better
Reduxing UI: Borrowing the Best of Web to Make Android Better
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 

Dumb and smart components + redux (1)

  • 1. Dumb and smart components & redux Brecht Billiet
  • 2. About me Brecht Billiet Frontend Architect - Coach - Trainer http://brecht.io @brechtbilliet
  • 3. Who still uses ng- controller and ng- include?
  • 4. Doesn’t that bother you? ● What HTML belongs to what JS? ● Where do the templates live? ● Absolute template-paths are not maintainable ● What logic belongs in a controller or directive? ● This template should contain... oh wait, there is an ng-include there. ● How do you pass data to your controllers?
  • 5. Think in components... ● 1 piece HTML + 1 piece LOGIC = COMPONENT ● You know it as: element directives ● Custom DOM tags ● A component has a single responsability ● We could use webcomponents + shadow DOM ● Sandboxed work ● Note: angular 1.5 has new component syntax
  • 6. What should you refactor to components?
  • 7.
  • 8. Everything is a component ● Your application is a tree of components ● Your pages are components ● Even your application is a component
  • 9. application Html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css
  • 10. application Html + js + css fooPage html + js + css html + js + css barPage html + js + css html + js + css html + js + css html + js + css html + js + css html + js + css Routingconfig: template: ‘<foo-page/>’ Routingconfig: template: ‘<bar-page/>’
  • 12. Other advantages ● No scope soup ● Nice hierarchy view ● Single responsability ● No more absolute paths ● Easier to reason about ● Easier to refactor ● Easier to upgrade to ng2
  • 13. What about attribute-directives? ● Let’s call them behaviors from now ● They add some logic to existing or custom components ● Separate them from your components folder
  • 14. DEMO
  • 17. Any rules of thumb?
  • 18.
  • 20. Keep them dumb where possible...
  • 21. Dumb component Smart component (container) Doesn’t know about the application Knows about the application Doesn’t do data fetching Does data fetching Main purpose is visualization Interacts the application Reusable Not reusable Only communicates with its direct parent Passes state and data to its children Doesn’t do state management Does state management
  • 24. State management ● State is very hard to manage in SPA’s ● State can be ○ data: results from REST calls ○ “sidebar is collapsed” or “spinner is active” ○ Which tab is currently active
  • 25. State management ● 3 types of state ○ Data state ○ Application state ○ Inner state (e.g value of textbox) We don’t have to persist nor manage that
  • 26. Why is it so hard to manage? ● 1-way databinding and 2-way databinding ● javascript references that are being broken and sometimes kept ● We can mutate the data in a million different ways ● No structure ● Data flows in different directions
  • 27. application state & logic state & logic state & logic state & logic state & logic state & logic state & logic state & logic state & logic state & logic Without flux
  • 28. 1 2 5 6 3 7 8 4 9 10 11 Without flux
  • 29. 1 2 5 6 3 7 8 4 9 10 11 I changed Without flux
  • 30. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I Without flux
  • 31. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 Without flux
  • 32. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 I have to subscribe before 8 sends a message Without flux
  • 33. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 I have to subscribe before 8 sends a message I’m sending an event upwards Without flux
  • 34. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 I have to subscribe before 8 sends a message I’m sending an event upwards I’m sending events downwards Without flux
  • 35. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 I have to subscribe before 8 sends a message I’m sending an event upwards Without flux I’m sending events downwards
  • 36. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 I have to subscribe before 8 sends a message I’m sending an event upwards I get my data from model x and I can update it here I get my data from model z Without flux I’m sending events downwards I get my data from model y
  • 37. 1 2 5 6 3 7 8 4 9 10 11 I changed So did I My changes should reflect 6 and 4 I have to subscribe before 8 sends a message I’m sending an event upwards I get my data from model x and I can update it here I get my data from model z Without flux My data is passed by twoway binding I’m sending events downwards I get my data from model y My data is passed by oneway binding My data is passed by twoway binding
  • 38. Where do those states/events come from?
  • 39. Who updated my state??
  • 41.
  • 42. Flux helps with that ● Brings structure in data flow ● Not a framework but an architecture by facebook ● Unidirectional dataflow, oneway databinding ● Data flows from the top down ● Easier to reason about state
  • 43. ● Action ○ Has an action type and a payload ● Dispatcher ○ Dispatches the actions to the store ● Store ○ Contains state ● View Components
  • 44. ● The unidirectional dataflow makes it easy to reason about
  • 45. I use Redux! Redux is based on flux, but it’s easier
  • 46. Redux ● Written by Dan Abramov (Facebook) ● Easier to use and less bloat ● Only has one store!! ● Completely Immutable data structure ● Uses reducers (pure functions) ● Flux approves
  • 48. 1 2 5 6 3 7 8 4 9 10 11 Me too Me too Me too Me too STORE I only send actions upwards to the store I’m the single- source-of-truth in your application!! ;-)
  • 49. 1 2 5 6 3 7 8 4 9 10 11 Me too Me too I’m dumb I’m dumb I’m dumb I’m dumb I’m dumb I’m dumb STORE Me too I only send actions upwards to the store Me too I’m the single- source-of-truth in your application!! ;-) Dumb components only notify their direct parents! No state management needed here :-)
  • 50. What’s different? ● We have one single source of truth: the store ● We don’t update state ourselves anymore, the store handles all the state of our application ● State is immutable ● Actions are always sent upwards
  • 51. What’s different? ● Dumb components don’t do state management ● State is being updated by pure functions (reducers) ● When the store is updated it notifies all the smart components
  • 53. Actions ● Actions have a type and a payload ● Keep the types together, gives clear overview
  • 55. Reducers ● Pure functions ● reducer(state: MyState, action: Action): MyState ● they update the state ● Completely immutable
  • 58. Middleware ● Easy logging, debugging ● Reproducible action state (pushable to server) ● Timetraveling with redux-devtools (requires react to be bundled in the angular project)
  • 59. Check it out ● egghead.io course (Free) ○ https://egghead.io/series/getting-started-with-redux
  • 60. Special thanks to ● Kwinten Pisman (@kwintenp) ● Jurgen Van de Moere (@jvandemo)
  • 62. One more thing Angular2, rxjs, redux, @ngrx/store, Typescript, webpack http://workshop.brecht.io @brechtbilliet Workshop June 2016