SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Downloaden Sie, um offline zu lesen
From User Action to
Framework Reaction
Reactivity in modern Frontend Frameworks
Jonas Bandi
jonas.bandi@ivorycode.com
Twitter: @jbandi
- Freelancer, in den letzten 6 Jahren vor allem in Projekten
im Spannungsfeld zwischen modernen Webentwicklung
und traditionellen Geschäftsanwendungen.
- Dozent an der Berner Fachhochschule seit 2007
- In-House Kurse:Web-Technologien im Enterprise
UBS, Postfinance, Mobiliar, SBB, BIT, BSI, Elca ...
JavaScript / Angular / React / Vue.js
Schulung / Beratung / Coaching / Reviews
jonas.bandi@ivorycode.com
http://ivorycode.com/#schulung
Agenda
Intro
Baseline:
"Out of the Box"-Reactivity of , and
(Slides and Demos)
Further Explorations:
Demos and Code Explorations
https://github.com/jbandi/framework-reactivity
Reactivity ?
Reactive Programming?
In computing, reactive programming is a declarative
programming paradigm concerned with data
streams and the propagation of change.
- Wikipedia
Reactive programming is programming
with asynchronous data streams.
Andre Staltz
The introduction to Reactive Programming you've been missing
https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
click$	
		.pipe(scan(count	=>	count	+	1,	0))	
		.subscribe(count	=>	console.log(`Clicked	${count}	times`));
In the Beginning there
was Darkness ...
... then the DOM was created.
... and we manipulated
the DOM ...
$(".menu-item")	
.removeClass("active")	
.addClass("inactive	")	
.css("padding-left",	"0px")	
.find(".trigger")	
.click(function(ev)	{	
			//	spaghetti	carbonara?	
})	
.each(function	()	{	
		 //	spaghetti	napoli?	
});
... the Dark Ages of DOM ...
... a new hope ...
Model View Controller
Client Side MVC
HTML
Browser
Model:
POJOs
Controller
View
DOM
Server
initial loadAJAX
Thou shalt not manipulate
the DOM!
the DOM *is* updated
State is Managed in JavaScript
Action:
change
state
Reaction:
re-render
Dom
Ajax
Timeout
...
User
trigger
State
Reactivity:The framework reacts on state
changes and updates the UI.
DOM
UI
EvanYou - Reactivity in Frontend JavaScript Frameworks: https://www.youtube.com/watch?v=r4pNEdIt_l4
http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
Reactivity:What and Why?
Traditional
"DOM-centric"
applications
UI = state
Browsers have "built-in" reactivity: If the DOM is
changed, the UI is re-rendered.
UI = f(state)
With modern SPA
architectures (MVC,
MVP, Components …) the
client state is
represented as
JavaScript objects.
The UI that you can see and manipulate
on screen is the result of painting a
visual representation of data.
Reactive programming in general addresses the question: How to
deal with change over time?
The UI should (automatically) re-render when the state changes.
When to call?
https://blog.danlew.net/2017/07/27/an-introduction-to-functional-reactive-programming/
https://twitter.com/Rich_Harris/status/1058751085706383362
Baseline: Core Reactivity
Zone.js
Change	
Detection
Angular Default Reactivity
@Component({	
		selector:	'app-counter',	
		template:	`	
				<div>	
						{{count}}	
				</div>	
		`	
})	
export	class	CounterComponent	implements	OnInit	{	
		count	=	0;	
		ngOnInit()	{	
				setInterval(()	=>	{	this.count++;	},	1000);	
		}	
}
Zone.js:
The "Magic" in Angular Change Detection
Zone.js is a JavaScript library provided by the Angular project that
patches many asynchronous browser APIs. Listeners can then be
triggered when these APIs are executed.
Angular relies on Zone.js to trigger automatic change detection.
Angular is running inside the NgZone (a zone created via
Zone.js).When async APIs are executed Angular gets notified
when the execution has finished and triggers change detection.
https://github.com/angular/zone.js/
https://medium.com/better-programming/zone-js-for-angular-devs-573d89bbb890
Patched APIs (examples): setTimeout, Promise, XMLHttpRequest, prompt
and DOM events.
More details: https://github.com/angular/angular/blob/master/packages/zone.js/STANDARD-APIS.md
Default Reactivity in Angular
UI = f(state)
Triggered by Zone.js
"simulated reactivity"
setInterval(	
1000);
Zone.js
"Zone to Angular":
Hey, there might be something going on ...
apply minimal
changes.
DOM
()	=>	this.count++
State
count:	42
let's check
everything ...
increase
<div>42</div>
change
detection
"simulated reactivity"
Default Change Detection
As default Angular detects changes by inspecting the state of all
components every time change detection is triggered.
Component
Component Component
Component Component
event
trigger change detection
on app
propagate change
detection to
children
propagate change
detection to
children
CD
CD
CD
CD
CD
Change detection is always
triggered at the top of the
component hirarchy and
propagates down to each child.
Every value used in a template
is inspected and compared to
the previous value.
Zone.js Checking all components
on every possible event can
be performance intense.
Default Reactivity in Angular
Zone.js with Default Change Detection:
• are a form of simulated reactivity: the framework
does not react to changes but to events that might
potentially resulted in changes
• are a form of transparent reactivity: It makes
reactivity an implicit characteristic of your program.
TheTaxonomy of Reactive Programming: https://vsavkin.com/the-taxonomy-of-reactive-programming-d40e2e23dee4
https://github.com/meteor/docs/blob/version-NEXT/long-form/tracker-manual.md
A common alternative in Angular is to model Reactivity
explicitly with RxJS, this is a form of explicit reactivity.
Default Angular Reactivity
Transparent Reactivity:
The programmer should be
able to use ideomatic
JavaScript, the Framework
does the rest.
Strength Weakness
Zone.js: Patching the browser is
problematic on many levels.
Brute-force approach of default
change detection is not optimal in
regard to performance.
Change Detection imposes
constraints ...
- avoid setter/getters
- unidirectional data-flow
- avoid async/await
"Simulated Reactivity"
Unidirectional Data Flow
Angular enforces unidirectional data flow from top to
bottom of the component tree.
• A child is not allowed to change the state of the parent once
the parent changes have been processed.
• This prevents cycles in the change detection.
(to prevent inconsistencies between state and ui and for better
performance)
• In development mode Angular performs a check (a second
change detection) to ensure that the component tree has
not changed after change detection. If there are changes it
throws a
ExpressionChangedAfterItHasBeenCheckedError.
https://indepth.dev/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error/
(Missing?) Reactivity in React
Function Components
function	AppComponent(props)	{	
return	(	
				<div>	
								<h1>{props.title}</h1>	
								<p>{props.message}</p>	
				</div>	
);	
}
properties
A components is a plain JavaScript function.
UI
(DOM ... )
The function is called each time the UI is
renedered (i.e. with every data-change)
A component transforms JavaScript state into the DOM structure:
UI = f(state)
import	React,	{	useEffect,	useState	}	from	'react';	
export	function	Counter()	{	
				const	[count,	setCount]	=	useState(0);	
				useEffect(()	=>	{	
								setInterval(()	=>	{	
												setCount(count	=>	count	+	1);	
								},	1000)	
				},	[]);	
				return	<div>{count}</div>	
}
React is used
to manage the
state
Reactivity in React
UI = f(state) triggered by
the programmer
setInterval(()	=>	
1000);
Programmer to React:
"Please change the state for me ... "
apply minimal changes.
DOM
setCount(count	=>	count	+	1),
State
count:	42
update the state
<div>42</div>
trigger re-renderingvirtual
DOM
React Reactivity
Functional Mindset:
- Rendering is a side-effect
of state changes.
- Components transform
state to ui.
Strength Weakness
"Render everything approach" is
wasteful.
State is managed by React: we
have to use the APIs and concepts
of React.
"Everything is rendered on every state change"
Reactive State in Vue
import	{	reactive,	watch	}	from	'vue'	
const	state	=	reactive({	
		count:	0	
})	
watch(()	=>	{	
		document.body.innerHTML	=	`count	is	${state.count}`	
})	
setInterval(()	=>	state.count++,	1000);
The example is using the composition API of upcoming vue 3:
https://vue-composition-api-rfc.netlify.com/
This is available inVue 2: https://github.com/vuejs/composition-api
changing state triggers re-rendering
Reactivity inVue
UI = f(state) triggered by
reactive state
setInterval(	
()	=>	state.count++,	
1000);
apply minimal changes.
DOM
State (Vue proxy)
{	count:	42	}
<div>42</div>
trigger re-rendering
get/set	count()
increase
ChangeTracking & Reactive State
https://vuejs.org/v2/guide/reactivity.html
Vue Reactivity
"True Reactivity":The state
can be observed.
Strength Weakness
State is not "plain" JavaScript, which
comes with its own limitations.
"Reactive State"
Alternatives &Variations
Zone.js
Change	
Detection
OnPush
Observables
Zone-Less
Angular Reactivity Options
Observables & async Pipe
UI$ = f(state$)
UI = f(state)
Reactivity realized with Streams
Triggered by Zone.js
"streams: true reactive programming"
"simulated reactivity"
Computed State
Anything that can be derived from the
application state, should be derived.
Automatically.
- MobX Introduction
No ideomatic
solution:
- getters/setters
- ngOnChanges
- Pure Pipe
- Observables
- (NgRx)
Derived state is
calculated during
rendering.
Optmization
with
Memoization.
Computed
properties.
State outside Components
Application-
State
Component
Tree
change
render
Application
A state container extracts the shared state out of the
components, and manages it in a global singleton.
The component tree becomes a big "view", any
component can access the state or trigger changes, no
matter where they are in the tree!
store
Code: https://github.com/jbandi/framework-reactivity
Have Fun with the
Framework of your Choice!
Twitter: @jbandi
Resources
• TheTaxonomy of Reactive Programming
https://vsavkin.com/the-taxonomy-of-reactive-programming-d40e2e23dee4
• Front end development and change detection (Angular vs. React):
https://www.youtube.com/watch?v=1i8klHov3vA
• JS Roundabout, Reactivity in React andVue, February 2018
https://www.youtube.com/watch?v=HWZq_rlJU4o
• Why React Is *Not* Reactive - Shawn Wang @ ReactNYC
https://www.youtube.com/watch?v=ZZoB5frlcnE
• Shift Dev 2019: "Rethinking Reactivity" - Rich Harris (NewYorkTimes)
https://www.youtube.com/watch?v=gJ2P6hGwcgo
• The Return of 'Write Less, Do More' by Rich Harris | JSCAMP 2019
https://www.youtube.com/watch?v=BzX4aTRPzno
• Reactivity:Vue 2 vsVue 3
https://www.vuemastery.com/blog/Reactivity-Vue2-vs-Vue3/

Weitere ähnliche Inhalte

Was ist angesagt?

AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation Phan Tuan
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJSWei Ru
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework Yakov Fain
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
Angular2 Development for Java developers
Angular2 Development for Java developersAngular2 Development for Java developers
Angular2 Development for Java developersYakov Fain
 
Implementing Data Visualization Apps on iOS Devices
Implementing Data Visualization Apps on iOS DevicesImplementing Data Visualization Apps on iOS Devices
Implementing Data Visualization Apps on iOS DevicesDouglass Turner
 
AngularJS Project Setup step-by- step guide - RapidValue Solutions
AngularJS Project Setup step-by- step guide - RapidValue SolutionsAngularJS Project Setup step-by- step guide - RapidValue Solutions
AngularJS Project Setup step-by- step guide - RapidValue SolutionsRapidValue
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - ServicesNir Kaufman
 
Introduction to Google Guice
Introduction to Google GuiceIntroduction to Google Guice
Introduction to Google GuiceKnoldus Inc.
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsVolodymyr Voytyshyn
 
Android Building, Testing and reversing
Android Building, Testing and reversingAndroid Building, Testing and reversing
Android Building, Testing and reversingEnrique López Mañas
 
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Codemotion
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized projectFabio Collini
 
AngularConf2016 - A leap of faith !?
AngularConf2016 - A leap of faith !?AngularConf2016 - A leap of faith !?
AngularConf2016 - A leap of faith !?Alessandro Giorgetti
 
Web polyglot programming
Web polyglot programmingWeb polyglot programming
Web polyglot programmingDmitry Buzdin
 

Was ist angesagt? (20)

AngularJs presentation
AngularJs presentation AngularJs presentation
AngularJs presentation
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Angular2 Development for Java developers
Angular2 Development for Java developersAngular2 Development for Java developers
Angular2 Development for Java developers
 
Implementing Data Visualization Apps on iOS Devices
Implementing Data Visualization Apps on iOS DevicesImplementing Data Visualization Apps on iOS Devices
Implementing Data Visualization Apps on iOS Devices
 
AngularJS Project Setup step-by- step guide - RapidValue Solutions
AngularJS Project Setup step-by- step guide - RapidValue SolutionsAngularJS Project Setup step-by- step guide - RapidValue Solutions
AngularJS Project Setup step-by- step guide - RapidValue Solutions
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Introduction to Google Guice
Introduction to Google GuiceIntroduction to Google Guice
Introduction to Google Guice
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design Patterns
 
AngularJS - TechTalk 3/2/2014
AngularJS - TechTalk 3/2/2014AngularJS - TechTalk 3/2/2014
AngularJS - TechTalk 3/2/2014
 
Android Building, Testing and reversing
Android Building, Testing and reversingAndroid Building, Testing and reversing
Android Building, Testing and reversing
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
 
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
Universal JavaScript Web Applications with React - Luciano Mammino - Codemoti...
 
Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
 
Using hilt in a modularized project
Using hilt in a modularized projectUsing hilt in a modularized project
Using hilt in a modularized project
 
AngularJs
AngularJsAngularJs
AngularJs
 
AngularConf2016 - A leap of faith !?
AngularConf2016 - A leap of faith !?AngularConf2016 - A leap of faith !?
AngularConf2016 - A leap of faith !?
 
Google Guice
Google GuiceGoogle Guice
Google Guice
 
Web polyglot programming
Web polyglot programmingWeb polyglot programming
Web polyglot programming
 

Ähnlich wie From User Action to Framework Reaction

From User Action to Framework Reaction
From User Action to Framework ReactionFrom User Action to Framework Reaction
From User Action to Framework Reactionjbandi
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)Chiew Carol
 
React gsg presentation with ryan jung &amp; elias malik
React   gsg presentation with ryan jung &amp; elias malikReact   gsg presentation with ryan jung &amp; elias malik
React gsg presentation with ryan jung &amp; elias malikLama K Banna
 
FRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JSFRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JSIRJET Journal
 
React vs angular (mobile first battle)
React vs angular (mobile first battle)React vs angular (mobile first battle)
React vs angular (mobile first battle)Michael Haberman
 
From Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) AgainFrom Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) Againjonknapp
 
Reactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xReactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xRam Maddali
 
NGRX Apps in Depth
NGRX Apps in DepthNGRX Apps in Depth
NGRX Apps in DepthTrayan Iliev
 
7 Redux challenges
7 Redux challenges7 Redux challenges
7 Redux challengesreactima
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaAli Muzaffar
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular jsAayush Shrestha
 
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)Binary Studio
 
React Native’s New Architecture Decoded: How to Migrate & Benefits?
React Native’s New Architecture Decoded: How to Migrate & Benefits? React Native’s New Architecture Decoded: How to Migrate & Benefits?
React Native’s New Architecture Decoded: How to Migrate & Benefits? jhonmiller20
 
vinod kumar JAVA
vinod kumar JAVAvinod kumar JAVA
vinod kumar JAVAVinod Kumar
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to AngularjsGaurav Agrawal
 

Ähnlich wie From User Action to Framework Reaction (20)

From User Action to Framework Reaction
From User Action to Framework ReactionFrom User Action to Framework Reaction
From User Action to Framework Reaction
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
React gsg presentation with ryan jung &amp; elias malik
React   gsg presentation with ryan jung &amp; elias malikReact   gsg presentation with ryan jung &amp; elias malik
React gsg presentation with ryan jung &amp; elias malik
 
FRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JSFRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JS
 
React vs angular (mobile first battle)
React vs angular (mobile first battle)React vs angular (mobile first battle)
React vs angular (mobile first battle)
 
From Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) AgainFrom Backbone to Ember and Back(bone) Again
From Backbone to Ember and Back(bone) Again
 
Angular Seminar-js
Angular Seminar-jsAngular Seminar-js
Angular Seminar-js
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
Reactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.xReactive microservices with eclipse vert.x
Reactive microservices with eclipse vert.x
 
NGRX Apps in Depth
NGRX Apps in DepthNGRX Apps in Depth
NGRX Apps in Depth
 
7 Redux challenges
7 Redux challenges7 Redux challenges
7 Redux challenges
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
 
DZone_RC_RxJS
DZone_RC_RxJSDZone_RC_RxJS
DZone_RC_RxJS
 
React Native’s New Architecture Decoded: How to Migrate & Benefits?
React Native’s New Architecture Decoded: How to Migrate & Benefits? React Native’s New Architecture Decoded: How to Migrate & Benefits?
React Native’s New Architecture Decoded: How to Migrate & Benefits?
 
vinod kumar JAVA
vinod kumar JAVAvinod kumar JAVA
vinod kumar JAVA
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
VenkateshVG
VenkateshVGVenkateshVG
VenkateshVG
 

Mehr von jbandi

Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?jbandi
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015jbandi
 
Java & JavaScript: Best Friends?
Java & JavaScript: Best Friends?Java & JavaScript: Best Friends?
Java & JavaScript: Best Friends?jbandi
 
There is something about JavaScript - Choose Forum 2014
There is something about JavaScript - Choose Forum 2014There is something about JavaScript - Choose Forum 2014
There is something about JavaScript - Choose Forum 2014jbandi
 
Professional JavaScript Development (An Introduction for Java Developers)
Professional JavaScript Development (An Introduction for Java Developers)Professional JavaScript Development (An Introduction for Java Developers)
Professional JavaScript Development (An Introduction for Java Developers)jbandi
 
vert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVMvert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVMjbandi
 
Kann JavaScript elegant sein?
Kann JavaScript elegant sein?Kann JavaScript elegant sein?
Kann JavaScript elegant sein?jbandi
 
NDC 2011 - Building .NET Applications with BDD
NDC 2011 - Building .NET Applications with BDDNDC 2011 - Building .NET Applications with BDD
NDC 2011 - Building .NET Applications with BDDjbandi
 
NDC 2011 - SpecFlow: Pragmatic BDD for .NET
NDC 2011 - SpecFlow: Pragmatic BDD for .NETNDC 2011 - SpecFlow: Pragmatic BDD for .NET
NDC 2011 - SpecFlow: Pragmatic BDD for .NETjbandi
 
Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?
Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?
Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?jbandi
 
Testing: Chances and Challenges in an agile World
Testing: Chances and Challenges in an agile WorldTesting: Chances and Challenges in an agile World
Testing: Chances and Challenges in an agile Worldjbandi
 

Mehr von jbandi (11)

Angular 2: What's New?
Angular 2: What's New?Angular 2: What's New?
Angular 2: What's New?
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015
 
Java & JavaScript: Best Friends?
Java & JavaScript: Best Friends?Java & JavaScript: Best Friends?
Java & JavaScript: Best Friends?
 
There is something about JavaScript - Choose Forum 2014
There is something about JavaScript - Choose Forum 2014There is something about JavaScript - Choose Forum 2014
There is something about JavaScript - Choose Forum 2014
 
Professional JavaScript Development (An Introduction for Java Developers)
Professional JavaScript Development (An Introduction for Java Developers)Professional JavaScript Development (An Introduction for Java Developers)
Professional JavaScript Development (An Introduction for Java Developers)
 
vert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVMvert.x - asynchronous event-driven web applications on the JVM
vert.x - asynchronous event-driven web applications on the JVM
 
Kann JavaScript elegant sein?
Kann JavaScript elegant sein?Kann JavaScript elegant sein?
Kann JavaScript elegant sein?
 
NDC 2011 - Building .NET Applications with BDD
NDC 2011 - Building .NET Applications with BDDNDC 2011 - Building .NET Applications with BDD
NDC 2011 - Building .NET Applications with BDD
 
NDC 2011 - SpecFlow: Pragmatic BDD for .NET
NDC 2011 - SpecFlow: Pragmatic BDD for .NETNDC 2011 - SpecFlow: Pragmatic BDD for .NET
NDC 2011 - SpecFlow: Pragmatic BDD for .NET
 
Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?
Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?
Testing Heute: ein Relikt aus dem Zeitalter des goldenen Wasserfalls?
 
Testing: Chances and Challenges in an agile World
Testing: Chances and Challenges in an agile WorldTesting: Chances and Challenges in an agile World
Testing: Chances and Challenges in an agile World
 

Kürzlich hochgeladen

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

From User Action to Framework Reaction

  • 1. From User Action to Framework Reaction Reactivity in modern Frontend Frameworks
  • 2. Jonas Bandi jonas.bandi@ivorycode.com Twitter: @jbandi - Freelancer, in den letzten 6 Jahren vor allem in Projekten im Spannungsfeld zwischen modernen Webentwicklung und traditionellen Geschäftsanwendungen. - Dozent an der Berner Fachhochschule seit 2007 - In-House Kurse:Web-Technologien im Enterprise UBS, Postfinance, Mobiliar, SBB, BIT, BSI, Elca ... JavaScript / Angular / React / Vue.js Schulung / Beratung / Coaching / Reviews jonas.bandi@ivorycode.com http://ivorycode.com/#schulung
  • 3. Agenda Intro Baseline: "Out of the Box"-Reactivity of , and (Slides and Demos) Further Explorations: Demos and Code Explorations https://github.com/jbandi/framework-reactivity
  • 5. Reactive Programming? In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. - Wikipedia Reactive programming is programming with asynchronous data streams. Andre Staltz The introduction to Reactive Programming you've been missing https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 click$ .pipe(scan(count => count + 1, 0)) .subscribe(count => console.log(`Clicked ${count} times`));
  • 6.
  • 7. In the Beginning there was Darkness ...
  • 8. ... then the DOM was created.
  • 9. ... and we manipulated the DOM ... $(".menu-item") .removeClass("active") .addClass("inactive ") .css("padding-left", "0px") .find(".trigger") .click(function(ev) { // spaghetti carbonara? }) .each(function () { // spaghetti napoli? });
  • 10. ... the Dark Ages of DOM ...
  • 11. ... a new hope ...
  • 14. Thou shalt not manipulate the DOM!
  • 15. the DOM *is* updated
  • 16. State is Managed in JavaScript Action: change state Reaction: re-render Dom Ajax Timeout ... User trigger State Reactivity:The framework reacts on state changes and updates the UI. DOM UI EvanYou - Reactivity in Frontend JavaScript Frameworks: https://www.youtube.com/watch?v=r4pNEdIt_l4 http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
  • 17. Reactivity:What and Why? Traditional "DOM-centric" applications UI = state Browsers have "built-in" reactivity: If the DOM is changed, the UI is re-rendered. UI = f(state) With modern SPA architectures (MVC, MVP, Components …) the client state is represented as JavaScript objects. The UI that you can see and manipulate on screen is the result of painting a visual representation of data. Reactive programming in general addresses the question: How to deal with change over time? The UI should (automatically) re-render when the state changes. When to call? https://blog.danlew.net/2017/07/27/an-introduction-to-functional-reactive-programming/
  • 22. Zone.js: The "Magic" in Angular Change Detection Zone.js is a JavaScript library provided by the Angular project that patches many asynchronous browser APIs. Listeners can then be triggered when these APIs are executed. Angular relies on Zone.js to trigger automatic change detection. Angular is running inside the NgZone (a zone created via Zone.js).When async APIs are executed Angular gets notified when the execution has finished and triggers change detection. https://github.com/angular/zone.js/ https://medium.com/better-programming/zone-js-for-angular-devs-573d89bbb890 Patched APIs (examples): setTimeout, Promise, XMLHttpRequest, prompt and DOM events. More details: https://github.com/angular/angular/blob/master/packages/zone.js/STANDARD-APIS.md
  • 23. Default Reactivity in Angular UI = f(state) Triggered by Zone.js "simulated reactivity" setInterval( 1000); Zone.js "Zone to Angular": Hey, there might be something going on ... apply minimal changes. DOM () => this.count++ State count: 42 let's check everything ... increase <div>42</div> change detection "simulated reactivity"
  • 24. Default Change Detection As default Angular detects changes by inspecting the state of all components every time change detection is triggered. Component Component Component Component Component event trigger change detection on app propagate change detection to children propagate change detection to children CD CD CD CD CD Change detection is always triggered at the top of the component hirarchy and propagates down to each child. Every value used in a template is inspected and compared to the previous value. Zone.js Checking all components on every possible event can be performance intense.
  • 25. Default Reactivity in Angular Zone.js with Default Change Detection: • are a form of simulated reactivity: the framework does not react to changes but to events that might potentially resulted in changes • are a form of transparent reactivity: It makes reactivity an implicit characteristic of your program. TheTaxonomy of Reactive Programming: https://vsavkin.com/the-taxonomy-of-reactive-programming-d40e2e23dee4 https://github.com/meteor/docs/blob/version-NEXT/long-form/tracker-manual.md A common alternative in Angular is to model Reactivity explicitly with RxJS, this is a form of explicit reactivity.
  • 26. Default Angular Reactivity Transparent Reactivity: The programmer should be able to use ideomatic JavaScript, the Framework does the rest. Strength Weakness Zone.js: Patching the browser is problematic on many levels. Brute-force approach of default change detection is not optimal in regard to performance. Change Detection imposes constraints ... - avoid setter/getters - unidirectional data-flow - avoid async/await "Simulated Reactivity"
  • 27. Unidirectional Data Flow Angular enforces unidirectional data flow from top to bottom of the component tree. • A child is not allowed to change the state of the parent once the parent changes have been processed. • This prevents cycles in the change detection. (to prevent inconsistencies between state and ui and for better performance) • In development mode Angular performs a check (a second change detection) to ensure that the component tree has not changed after change detection. If there are changes it throws a ExpressionChangedAfterItHasBeenCheckedError. https://indepth.dev/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error/
  • 29. Function Components function AppComponent(props) { return ( <div> <h1>{props.title}</h1> <p>{props.message}</p> </div> ); } properties A components is a plain JavaScript function. UI (DOM ... ) The function is called each time the UI is renedered (i.e. with every data-change) A component transforms JavaScript state into the DOM structure: UI = f(state)
  • 31. Reactivity in React UI = f(state) triggered by the programmer setInterval(() => 1000); Programmer to React: "Please change the state for me ... " apply minimal changes. DOM setCount(count => count + 1), State count: 42 update the state <div>42</div> trigger re-renderingvirtual DOM
  • 32.
  • 33. React Reactivity Functional Mindset: - Rendering is a side-effect of state changes. - Components transform state to ui. Strength Weakness "Render everything approach" is wasteful. State is managed by React: we have to use the APIs and concepts of React. "Everything is rendered on every state change"
  • 35. import { reactive, watch } from 'vue' const state = reactive({ count: 0 }) watch(() => { document.body.innerHTML = `count is ${state.count}` }) setInterval(() => state.count++, 1000); The example is using the composition API of upcoming vue 3: https://vue-composition-api-rfc.netlify.com/ This is available inVue 2: https://github.com/vuejs/composition-api changing state triggers re-rendering
  • 36. Reactivity inVue UI = f(state) triggered by reactive state setInterval( () => state.count++, 1000); apply minimal changes. DOM State (Vue proxy) { count: 42 } <div>42</div> trigger re-rendering get/set count() increase
  • 37. ChangeTracking & Reactive State https://vuejs.org/v2/guide/reactivity.html
  • 38. Vue Reactivity "True Reactivity":The state can be observed. Strength Weakness State is not "plain" JavaScript, which comes with its own limitations. "Reactive State"
  • 41. Observables & async Pipe UI$ = f(state$) UI = f(state) Reactivity realized with Streams Triggered by Zone.js "streams: true reactive programming" "simulated reactivity"
  • 42. Computed State Anything that can be derived from the application state, should be derived. Automatically. - MobX Introduction No ideomatic solution: - getters/setters - ngOnChanges - Pure Pipe - Observables - (NgRx) Derived state is calculated during rendering. Optmization with Memoization. Computed properties.
  • 43. State outside Components Application- State Component Tree change render Application A state container extracts the shared state out of the components, and manages it in a global singleton. The component tree becomes a big "view", any component can access the state or trigger changes, no matter where they are in the tree! store
  • 44. Code: https://github.com/jbandi/framework-reactivity Have Fun with the Framework of your Choice! Twitter: @jbandi
  • 45. Resources • TheTaxonomy of Reactive Programming https://vsavkin.com/the-taxonomy-of-reactive-programming-d40e2e23dee4 • Front end development and change detection (Angular vs. React): https://www.youtube.com/watch?v=1i8klHov3vA • JS Roundabout, Reactivity in React andVue, February 2018 https://www.youtube.com/watch?v=HWZq_rlJU4o • Why React Is *Not* Reactive - Shawn Wang @ ReactNYC https://www.youtube.com/watch?v=ZZoB5frlcnE • Shift Dev 2019: "Rethinking Reactivity" - Rich Harris (NewYorkTimes) https://www.youtube.com/watch?v=gJ2P6hGwcgo • The Return of 'Write Less, Do More' by Rich Harris | JSCAMP 2019 https://www.youtube.com/watch?v=BzX4aTRPzno • Reactivity:Vue 2 vsVue 3 https://www.vuemastery.com/blog/Reactivity-Vue2-vs-Vue3/