SlideShare a Scribd company logo
From User Action to
Framework Reaction
Reactivity in modern Frontend Frameworks
Jonas Bandi
@jbandi
- Freelancer, in den letzten 8 Jahren vor allem in Projekten
im Spannungsfeld zwischen modernen Webentwicklung
und traditionellen Geschäftsanwendungen.
- Dozent an der Berner Fachhochschule seit 2007
- In-House Kurse & Beratungen zu Web-Technologien im
Enterprise: UBS, Post
fi
nance, Mobiliar,AXA, BIT, SBB, Elca,
Adnovum, BSI ...
JavaScript / Angular / React / Vue.js
Schulung / Beratung / Coaching / Reviews
jonas.bandi@ivorycode.com
Reactivity ?
"There are as many de
fi
nitions of
reactive programming as there are
reactive programmers."
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.
-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`));
reactive programming is a paradigm in which declarative code
is issued to construct asynchronous processing pipelines.
- De
fi
ning the term "reactive"
https://developer.ibm.com/articles/de
fi
ning-the-term-reactive/
"The essence of functional reactive
programming is to specify the
dynamic behavior of a value
completely at the time of declaration"
- Heinrich Apfelmus, via Michel Westrate
https://www.youtube.com/watch?v=AdNJ3fydeao
Agenda
Reactivity - What are we talking about here?
• Code Example
• How does it work?
• Implications
• One Advantage
• One Problem
A glimpse into each framework.
A "feeling" how the framework works.
Int
ro
E
xp
lorat
ion
"Out of the Box"-Reactivity of
T
a
k
e
a
w
a
y
State of JavaScript Survey 2021:
https://2021.stateofjs.com/en-US/libraries/front-end-frameworks
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
Brow
ser
Model:
POJOs
Controller
View
DOM
Server
initial request
AJAX
Thou shalt not manipulate
the DOM!
the DOM *is* updated
UI (DOM)
Action
Reaction:
re-render
Ajax
Timeout
...
User
t
r
i
g
g
e
r
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
State (JavaScript)
The UI renders the state and "signals" events.
change
st
at
e
(asynchronous)
Reactivity in a SPA:The application reacts on
state changes and updates the UI.
State is Managed in JavaScript
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 client-side
Single-Page-
Applications, the
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.
This is the Reactivity we are investigating:
How do frameworks deal with state changes over time?
The UI should (automatically) update when the state changes.
When to call?
Problem: the same state might be displayed at several places in the DOM.
https://twitter.com/Rich_Harris/status/1058751085706383362
Framework Reactivity
Zone.js
Change
Detection
Angular Reactivity
🪄 setInterval 🪄
It's not what you think it is ...
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 noti
fi
ed
when the execution has
fi
nished 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"
Mutability
Change Detection Cascade
Default Reactivity in Angular
Zone.js with Default Change Detection:
• is a form of simulated reactivity: the framework does
not react to changes but to events that might
potentially have caused changes
• is 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.
Programming model based
on mutations.
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 / rules ...
- unidirectional data-
fl
ow
- avoid setter/getters?
- no native async/await
"Simulated Reactivity"
Angular ReactivityVariations
ChangeDetectionStrategy.OnPush
Zone-Less
Observables
(Missing?) Reactivity in React
Function Components
function AppComponent(props) {
return (
<div>
<h1>{props.title}</h1>
<p>{props.message}</p>
</div>
);
}
data
Components are written as plain JavaScript functions.
UI
(DOM ... )
The function is called each time the UI is
rendered (i.e. with every data-change)
AVisual GuideTo React Mental Models:
https://obedparla.com/code/a-visual-guide-to-react-mental-models/
TheVirtual DOM
Components Virtual DOM Browser DOM
body
div
h1 div
button
input
<body>
<div>
<h1></h1>
<div>
<input/>
<button>
</button>
</div>
</div>
</body>
App
Title
Content
input
button
diff
patch
render
TheVirtual DOM also enables server-side rendering and
rendering to iOS/Android UIs.
In-Memory, implemented in JavaScript
reconciliation
https://reactjs.org/docs/reconciliation.html
if the state changes
components are
rendered completely
including all their
children
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-rendering
virtual
DOM
Immutability
Render Cascade
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.
Programming model enforces
"immutable state management".
"Everything is rendered on every state change"
https://www.joshwcomeau.com/react/why-react-re-renders/
https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/
Reactive State in Vue
The example is using the Composition API ofVue 3:
https://vuejs.org/api/reactivity-core.html
changing state
triggers re-rendering
"Naked" Reactive State inVue:
Reactivity inVue
UI = f (state ) triggered by
reactive state
setInterval(
() =>
1000);
apply minimal changes.
DOM
State (Vue proxy)
{ count: 42 }
<div>42</div>
trigger fine-grained
re-rendering
get/set count()
increase
A Hands-on Introduction to Fine-Grained Reactivity: https://dev.to/ryansolid/a-hands-on-introduction-to-
fi
ne-grained-reactivity-3ndf
Note: Some statemanagment libraries implement the same concept for other frameworks (MobX, Jotai, Signals ...)
++,
X X X
UI = f (state )
Y Y Y
UI = f (state )
Z Z Z
state.count
f
i
n
e
-
g
r
a
i
n
e
d
ChangeTracking & Reactive State
https://vuejs.org/v2/guide/reactivity.html
Vue Reactivity
"True Reactivity":The state
can be observed.
Fine-Grained Reactivity: only
runs the code that need to
be run.
Programming model
embraces mutability.
Strength Weakness
State is not "plain" JavaScript, which
comes with its own limitations.
"Reactive State"
https://dev.to/ryansolid/a-hands-on-introduction-to-
fi
ne-grained-reactivity-3ndf
Svelte
"Embrace the Compiler!"
aka:Abandon JavaScript?
At compile time. Svelte
generates code to
manipulate the D0M
at runtime.
Reactivity in Svelte
setInterval(
() => state.count++
1000);
apply minimal
changes.
DOM
<div>42</div>
UI = f (state )
triggered by
compile-time generated
code
X X X
UI = f (state )
Y Y Y
UI = f (state )
Z Z Z
f
i
n
e
-
g
r
a
i
n
e
d
setInterval(
() => {
state.count++;
$invalidate(state);
}
1000);
function $invalidate(args){
...
updateElement(el, newVal)
}
call
compile
generate
build-time run-time
svelte helper
functions
Compile-Time-Generated Reactivity
<script>
let name = 'Jonas';
let number = 0;
function update(e) {
name = 'Bandi';
number = 42;
}
</script>
<h1 on:click={update}>
Hello {name} {number}!
</h1>
import { SvelteComponent, append, detach, element, init, insert,
listen, noop, safe_not_equal, set_data, space, text} from "svelte/internal";
function create_fragment(ctx) {
let h1;
let t0;
let t1;
let t2;
let t3;
let t4;
let mounted;
let dispose;
return {
c() {
h1 = element("h1");
t0 = text("Hello ");
t1 = text(/*name*/ ctx[0]);
t2 = space();
t3 = text(/*number*/ ctx[1]);
t4 = text("!");
},
m(target, anchor) {
insert(target, h1, anchor);
append(h1, t0);
append(h1, t1);
append(h1, t2);
append(h1, t3);
append(h1, t4);
if (!mounted) {
dispose = listen(h1, "click", /*update*/ ctx[2]);
mounted = true;
}
},
p(ctx, [dirty]) {
if (dirty & /*name*/ 1) set_data(t1, /*name*/ ctx[0]);
if (dirty & /*number*/ 2) set_data(t3, /*number*/ ctx[1]);
},
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(h1);
mounted = false;
dispose();
}
};
}
function instance($$self, $$props, $$invalidate) {
let name = 'Jonas';
let number = 0;
function update(e) {
$$invalidate(0, name = 'Bandi');
$$invalidate(1, number = 42);
}
return [name, number, update];
}
class App extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance,
create_fragment, safe_not_equal, {});
}
}
export default App;
compiler
helper functions
life-cycle
m
o
u
n
t
c
r
e
a
t
e
u
n
m
o
u
n
t
u
p
d
a
t
e
instance scope
initialization
"r
e
a
c
t
iv
it
y"
Svelte Reactivity
Very compact and intuitive
code.
Fine-Grained Reactivity: only
runs the code that need to
be run.
Signi
fi
cantly faster than the
other mainstream
frameworks.
Strength Weakness
"extending" the semantics of
JavaScript
"Compile-Time-Generated Reactivity"
Circling Back:
All Modern Frontend Frameworks are Compilers!
Angular
• TypeScript
• Template
• (workarounds)
React
• JSX
• TypeScript
Vue
• SFC
• Template
• TypeScript
... but Svelte goes one step further.
"React Forget" - A Memoizing Compiler
https://www.youtube.com/watch?v=lGEMwh32soc
https://reactjs.org/blog/2022/06/15/react-labs-what-we-have-been-working-on-june-2022.html
Vue ReactivityTransforms
https://vuejs.org/guide/extras/reactivity-transform.html
https://github.com/vuejs/rfcs/discussions/369
React anVue have plans for future compiler features
that are changing the samantics of JavaScript ...
(reducing biolerplate for convenience)
Fun Fact:
Code: https://github.com/jbandi/framework-reactivity-2022
Have Fun with the
Framework of your Choice!
@jbandi
QUESTIONS?
JavaScript / Angular / React / Vue.js
Schulung / Beratung / Coaching / Reviews
jonas.bandi@ivorycode.com

More Related Content

Similar to From User Action to Framework Reaction

Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
Aayush Shrestha
 
7 Redux challenges
7 Redux challenges7 Redux challenges
7 Redux challenges
reactima
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & Angular
Alexe Bogdan
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
Remo Jansen
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
Jitendra Kumar
 
Mvc ppt
Mvc pptMvc ppt
Mvc ppt
Gowarthini
 
FRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JSFRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JS
IRJET Journal
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
Jordan Open Source Association
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
Ali Sa'o
 
20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download
Mohd Quasim
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
Bethmi Gunasekara
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
Gaurav Agrawal
 
React intro
React introReact intro
React intro
PushkarThakur7
 
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
jonknapp
 
My perspective on MVP and architecture discussions
My perspective on MVP and architecture discussionsMy perspective on MVP and architecture discussions
My perspective on MVP and architecture discussions
Paul Blundell
 
React JS Components & Its Importance.docx
React JS Components & Its Importance.docxReact JS Components & Its Importance.docx
React JS Components & Its Importance.docx
React Masters
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
jobinThomas54
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
Angular Seminar-js
Angular Seminar-jsAngular Seminar-js
Angular Seminar-js
Mindfire Solutions
 
A Big Picture Of AngularJS
A Big Picture Of AngularJSA Big Picture Of AngularJS
A Big Picture Of AngularJS
Nitin Pandit
 

Similar to From User Action to Framework Reaction (20)

Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
7 Redux challenges
7 Redux challenges7 Redux challenges
7 Redux challenges
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & Angular
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Mvc ppt
Mvc pptMvc ppt
Mvc ppt
 
FRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JSFRONTEND DEVELOPMENT WITH REACT.JS
FRONTEND DEVELOPMENT WITH REACT.JS
 
JOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and ReduxJOSA TechTalks - Better Web Apps with React and Redux
JOSA TechTalks - Better Web Apps with React and Redux
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download20 React Interview Questions. Pdf Download
20 React Interview Questions. Pdf Download
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
React intro
React introReact intro
React intro
 
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
 
My perspective on MVP and architecture discussions
My perspective on MVP and architecture discussionsMy perspective on MVP and architecture discussions
My perspective on MVP and architecture discussions
 
React JS Components & Its Importance.docx
React JS Components & Its Importance.docxReact JS Components & Its Importance.docx
React JS Components & Its Importance.docx
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
Angular Seminar-js
Angular Seminar-jsAngular Seminar-js
Angular Seminar-js
 
A Big Picture Of AngularJS
A Big Picture Of AngularJSA Big Picture Of AngularJS
A Big Picture Of AngularJS
 

More from 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 2015
jbandi
 
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 2014
jbandi
 
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 JVM
jbandi
 
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 BDD
jbandi
 
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
jbandi
 
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 World
jbandi
 

More from 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
 

Recently uploaded

UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
AnkitaPandya11
 
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
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
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
 
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
 
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
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
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
 
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
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 

Recently uploaded (20)

UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
 
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
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
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
 
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
 
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
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
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
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 

From User Action to Framework Reaction

  • 1. From User Action to Framework Reaction Reactivity in modern Frontend Frameworks
  • 2. Jonas Bandi @jbandi - Freelancer, in den letzten 8 Jahren vor allem in Projekten im Spannungsfeld zwischen modernen Webentwicklung und traditionellen Geschäftsanwendungen. - Dozent an der Berner Fachhochschule seit 2007 - In-House Kurse & Beratungen zu Web-Technologien im Enterprise: UBS, Post fi nance, Mobiliar,AXA, BIT, SBB, Elca, Adnovum, BSI ... JavaScript / Angular / React / Vue.js Schulung / Beratung / Coaching / Reviews jonas.bandi@ivorycode.com
  • 3. Reactivity ? "There are as many de fi nitions of reactive programming as there are reactive programmers."
  • 4. 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. -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`)); reactive programming is a paradigm in which declarative code is issued to construct asynchronous processing pipelines. - De fi ning the term "reactive" https://developer.ibm.com/articles/de fi ning-the-term-reactive/
  • 5. "The essence of functional reactive programming is to specify the dynamic behavior of a value completely at the time of declaration" - Heinrich Apfelmus, via Michel Westrate https://www.youtube.com/watch?v=AdNJ3fydeao
  • 6.
  • 7. Agenda Reactivity - What are we talking about here? • Code Example • How does it work? • Implications • One Advantage • One Problem A glimpse into each framework. A "feeling" how the framework works. Int ro E xp lorat ion "Out of the Box"-Reactivity of T a k e a w a y
  • 8. State of JavaScript Survey 2021: https://2021.stateofjs.com/en-US/libraries/front-end-frameworks
  • 9. In the Beginning there was Darkness ...
  • 10. ... then the DOM was created.
  • 11. ... 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? });
  • 12. ... the Dark Ages of DOM ...
  • 13. ... a new hope ...
  • 16. Thou shalt not manipulate the DOM!
  • 17. the DOM *is* updated
  • 18. UI (DOM) Action Reaction: re-render Ajax Timeout ... User t r i g g e r 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 State (JavaScript) The UI renders the state and "signals" events. change st at e (asynchronous) Reactivity in a SPA:The application reacts on state changes and updates the UI. State is Managed in JavaScript
  • 19. 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 client-side Single-Page- Applications, the 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. This is the Reactivity we are investigating: How do frameworks deal with state changes over time? The UI should (automatically) update when the state changes. When to call? Problem: the same state might be displayed at several places in the DOM.
  • 23.
  • 24. 🪄 setInterval 🪄 It's not what you think it is ...
  • 25. 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 noti fi ed when the execution has fi nished 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
  • 26. 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"
  • 29. Default Reactivity in Angular Zone.js with Default Change Detection: • is a form of simulated reactivity: the framework does not react to changes but to events that might potentially have caused changes • is 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.
  • 30. Default Angular Reactivity Transparent Reactivity: The programmer should be able to use ideomatic JavaScript, the Framework does the rest. Programming model based on mutations. 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 / rules ... - unidirectional data- fl ow - avoid setter/getters? - no native async/await "Simulated Reactivity"
  • 33. Function Components function AppComponent(props) { return ( <div> <h1>{props.title}</h1> <p>{props.message}</p> </div> ); } data Components are written as plain JavaScript functions. UI (DOM ... ) The function is called each time the UI is rendered (i.e. with every data-change) AVisual GuideTo React Mental Models: https://obedparla.com/code/a-visual-guide-to-react-mental-models/
  • 34. TheVirtual DOM Components Virtual DOM Browser DOM body div h1 div button input <body> <div> <h1></h1> <div> <input/> <button> </button> </div> </div> </body> App Title Content input button diff patch render TheVirtual DOM also enables server-side rendering and rendering to iOS/Android UIs. In-Memory, implemented in JavaScript reconciliation https://reactjs.org/docs/reconciliation.html if the state changes components are rendered completely including all their children
  • 35. React is used to manage the state
  • 36.
  • 37. 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-rendering virtual DOM
  • 40. 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. Programming model enforces "immutable state management". "Everything is rendered on every state change" https://www.joshwcomeau.com/react/why-react-re-renders/ https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/
  • 42.
  • 43. The example is using the Composition API ofVue 3: https://vuejs.org/api/reactivity-core.html changing state triggers re-rendering "Naked" Reactive State inVue:
  • 44. Reactivity inVue UI = f (state ) triggered by reactive state setInterval( () => 1000); apply minimal changes. DOM State (Vue proxy) { count: 42 } <div>42</div> trigger fine-grained re-rendering get/set count() increase A Hands-on Introduction to Fine-Grained Reactivity: https://dev.to/ryansolid/a-hands-on-introduction-to- fi ne-grained-reactivity-3ndf Note: Some statemanagment libraries implement the same concept for other frameworks (MobX, Jotai, Signals ...) ++, X X X UI = f (state ) Y Y Y UI = f (state ) Z Z Z state.count f i n e - g r a i n e d
  • 45. ChangeTracking & Reactive State https://vuejs.org/v2/guide/reactivity.html
  • 46. Vue Reactivity "True Reactivity":The state can be observed. Fine-Grained Reactivity: only runs the code that need to be run. Programming model embraces mutability. Strength Weakness State is not "plain" JavaScript, which comes with its own limitations. "Reactive State" https://dev.to/ryansolid/a-hands-on-introduction-to- fi ne-grained-reactivity-3ndf
  • 48. At compile time. Svelte generates code to manipulate the D0M at runtime.
  • 49. Reactivity in Svelte setInterval( () => state.count++ 1000); apply minimal changes. DOM <div>42</div> UI = f (state ) triggered by compile-time generated code X X X UI = f (state ) Y Y Y UI = f (state ) Z Z Z f i n e - g r a i n e d setInterval( () => { state.count++; $invalidate(state); } 1000); function $invalidate(args){ ... updateElement(el, newVal) } call compile generate build-time run-time svelte helper functions
  • 50. Compile-Time-Generated Reactivity <script> let name = 'Jonas'; let number = 0; function update(e) { name = 'Bandi'; number = 42; } </script> <h1 on:click={update}> Hello {name} {number}! </h1> import { SvelteComponent, append, detach, element, init, insert, listen, noop, safe_not_equal, set_data, space, text} from "svelte/internal"; function create_fragment(ctx) { let h1; let t0; let t1; let t2; let t3; let t4; let mounted; let dispose; return { c() { h1 = element("h1"); t0 = text("Hello "); t1 = text(/*name*/ ctx[0]); t2 = space(); t3 = text(/*number*/ ctx[1]); t4 = text("!"); }, m(target, anchor) { insert(target, h1, anchor); append(h1, t0); append(h1, t1); append(h1, t2); append(h1, t3); append(h1, t4); if (!mounted) { dispose = listen(h1, "click", /*update*/ ctx[2]); mounted = true; } }, p(ctx, [dirty]) { if (dirty & /*name*/ 1) set_data(t1, /*name*/ ctx[0]); if (dirty & /*number*/ 2) set_data(t3, /*number*/ ctx[1]); }, i: noop, o: noop, d(detaching) { if (detaching) detach(h1); mounted = false; dispose(); } }; } function instance($$self, $$props, $$invalidate) { let name = 'Jonas'; let number = 0; function update(e) { $$invalidate(0, name = 'Bandi'); $$invalidate(1, number = 42); } return [name, number, update]; } class App extends SvelteComponent { constructor(options) { super(); init(this, options, instance, create_fragment, safe_not_equal, {}); } } export default App; compiler helper functions life-cycle m o u n t c r e a t e u n m o u n t u p d a t e instance scope initialization "r e a c t iv it y"
  • 51. Svelte Reactivity Very compact and intuitive code. Fine-Grained Reactivity: only runs the code that need to be run. Signi fi cantly faster than the other mainstream frameworks. Strength Weakness "extending" the semantics of JavaScript "Compile-Time-Generated Reactivity"
  • 52. Circling Back: All Modern Frontend Frameworks are Compilers! Angular • TypeScript • Template • (workarounds) React • JSX • TypeScript Vue • SFC • Template • TypeScript ... but Svelte goes one step further.
  • 53. "React Forget" - A Memoizing Compiler https://www.youtube.com/watch?v=lGEMwh32soc https://reactjs.org/blog/2022/06/15/react-labs-what-we-have-been-working-on-june-2022.html Vue ReactivityTransforms https://vuejs.org/guide/extras/reactivity-transform.html https://github.com/vuejs/rfcs/discussions/369 React anVue have plans for future compiler features that are changing the samantics of JavaScript ... (reducing biolerplate for convenience) Fun Fact:
  • 54. Code: https://github.com/jbandi/framework-reactivity-2022 Have Fun with the Framework of your Choice! @jbandi
  • 55. QUESTIONS? JavaScript / Angular / React / Vue.js Schulung / Beratung / Coaching / Reviews jonas.bandi@ivorycode.com