Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
React JS intro
Search
Gabriele Petronella
September 30, 2015
Programming
3
380
React JS intro
A lightweight introduction to React JS in the context of web applications
Gabriele Petronella
September 30, 2015
Tweet
Share
More Decks by Gabriele Petronella
See All by Gabriele Petronella
Design System Adventures in React - ReactJS Day 2024
gabro
0
88
Design System Adventures in React
gabro
1
91
Casting Metals
gabro
0
360
Functional Programming in front-end applications
gabro
1
230
Functional Programming in Front-end Applications
gabro
3
170
How to get away with Functional Programming in front-end applications
gabro
3
1.4k
Bridging the tooling gap with Scala.js
gabro
0
270
Monad Transformers Down to Earth
gabro
2
2.6k
Move fast and fix things
gabro
0
330
Other Decks in Programming
See All in Programming
CSC307 Lecture 17
javiergs
PRO
0
120
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
36
23k
無関心の谷
kanayannet
0
170
赤裸々に公開。 TSKaigiのオフシーズン
takezoux2
0
130
ワンバイナリWebサービスのススメ
mackee
10
7.7k
The Evolution of Enterprise Java with Jakarta EE 11 and Beyond
ivargrimstad
1
770
Datadog RUM 本番導入までの道
shinter61
1
290
Javaのルールをねじ曲げろ!禁断の操作とその代償から学ぶメタプログラミング入門 / A Guide to Metaprogramming: Lessons from Forbidden Techniques and Their Price
nrslib
3
2k
セキュリティマネジャー廃止とクラウドネイティブ型サンドボックス活用
kazumura
1
180
A2A プロトコルを試してみる
azukiazusa1
1
180
Development of an App for Intuitive AI Learning - Blockly Summit 2025
teba_eleven
0
120
Select API from Kotlin Coroutine
jmatsu
1
170
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
269
20k
GraphQLとの向き合い方2022年版
quramy
46
14k
How to Ace a Technical Interview
jacobian
276
23k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
We Have a Design System, Now What?
morganepeng
52
7.6k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Git: the NoSQL Database
bkeepers
PRO
430
65k
Balancing Empowerment & Direction
lara
1
320
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
46
9.6k
Transcript
AN INTRODUCTION TO REACTJS GABRIELE PETRONELLA SOFTWARE ENGINEER @ BUILDO
TWITTER: @GABRO27 / @BUILDOHQ
[email protected]
None
HTML
HTML <!DOCTYPE html> <html> <head> <title>This is a title</title> </head>
<body> <p>Hello world!</p> </body> </html>
I just had to take the hypertext idea and connect
it to the TCP and DNS ideas and — ta- da!— the World Wide Web — Tim Berners Lee
THE WEB, IN THE 90S ╔═══════════╗ ---------- ║ ║ gimme
dat page ║ ║----------------------> ║ browser ║ server ║ ║ <html>...</html> ║ ║<---------------------- ╚═══════════╝ ----------
None
<a href="/somewhere-fun">LINK!</a>
╔═══════════╗ ║ ║ gimme dat page ║
║----------------------> ║ ║ ║ ║ <html>...</html> ║ ║<---------------------- ║ browser ║ ... server ║ ║ ║ ║ do dat thing ║ ║----------------------> ║ ║ ║ ║ <html>...</html> ║ ║<---------------------- ║ ║ ╚═══════════╝
None
ADDING AN ELEMENT TO A CART <ul> <ul> <li>An apple</li>
<li>An apple</li> <li>A horse</li> ---> <li>A horse</li> </ul> <li>A dragon</li> </ul>
None
JAVASCRIPT (1995)
None
ADDING AN ELEMENT TO A CART var ul = document.getElementById("cart");
var li = document.createElement("li"); li.appendChild(document.createTextNode("Four")); ul.appendChild(li);
╔═══════════╗ ║ ║ gimme dat page ║
║----------------------> ║ ║ <html>...</html> ║ browser ║<---------------------- ║ ║ script.js ║ ║<---------------------- server ║ ║─────┐ ╚═══════════╝ ! ▲ ! <html>...</html>! !add item ! ! "########$ ! ! ! ! ! script !----- ! ! %########&
None
SO YOU SAY WE CAN CREATE HTML ELEMENTS...
SPA SINGLE-PAGE APPLICATIONS
╔═══════════╗ ║ ║ gimme dat page ║
║----------------------> ║ ║ <html>...</html> ║ browser ║<---------------------- ║ ║ app.js ║ ║<---------------------- ║ ╠─────┐ ╚═══════════╝ ! ▲ ! server <html>...</html>! !do stuff ! ! "##########$ ! ! !<----- ! ! gimme data ! app.js !----------------------> ! ! ! ! { data: "blah", ... } ! !<---------------------- %##########&
THE USUAL SUSPECTS
SOUNDS GREAT BUT...
COMPLEXITY!
MUTABLE STATE
OVER THE LAST 25 YEARS WE'VE SEEN...
FLUSH THE PAGE AT EVERY CHANGE VS COMPUTE THE CHANGES
LOCALLY
HERE'S HOW THE PAGE SHOULD LOOK LIKE VS HERE'S HOW
TO MAKE IT
IN OTHER WORDS...
DECLARATIVE VS IMPERATIVE
AND POTENTIALLY...
DEVELOPER EXPERIENCE VS USER EXPERIENCE
MEET REACT
DX + UX = REACT
DECLARATIVE APPROACH CONCEPTUALLY RE-RENDERING EVERYTHING EVERYTIME
IMPERATIVE EXPERIENCE MUTATION HAPPENS BEHIND THE SCENE
MVC
IT'S ALL ABOUT REUSABLE COMPONENTS
None
OUR FIRST COMPONENT const Hello = React.createClass({ render() { return
<div>Hello!</div>; } });
WAIT! HTML INSIDE JAVASCRIPT?!
YES, ALMOST...
JSX <div>Hello</div> gets translated to React.createElement("div", null, "Hello");
Details aside, there's no separation between templates and logic
SEPARATE CONCERNS NOT TECHNOLOGIES
ONLY TWO THINGS CAN AFFECT A COMPONENT > props !
> state "
PROPS ! A generalization over HTML attributes <button className='button inactive'>Click</button>
gets translated to React.createElement( "button", { className: 'button inactive' }, // <--- props "Click" );
OUR FIRST COMPONENT ACCEPTING PROPS const Greeter = React.createClass({ render()
{ return <div>Hello {this.props.name}!</div>; } ^ }); | just a javascript variable
AND THEN USE IT LIKE <Greeter name='Gabriele' /> ^ |_____________
passing a prop
CAN THIS BE EFFICIENT?
YOU WRITE render() { return ( <div> <span>Hello {this.props.userName}!</span> </div>
); }
REACT COMPUTES renderA: <div><span>Hello Gabriele!</span></div> renderB: <div><span>Hello Irina!</span></div> => [replaceAttribute
textContent 'Hello Irina'] ^ | | a state mutation, i.e. the horrible thing you want to avoid writing by hand
STATE ! A component can have an internal state AVOID
WHEN YOU CAN!
OUR FIRST STATEFUL COMPONENT !
const Counter = React.createClass({ getInitialState() { return { count: 0
}; }, increaseCount() { this.setState({ count: this.state.count + 1 }); }, ^ |_______________ triggers a re-render render() { return ( <div> <span>{this.state.count}</span> <button onClick={this.increaseCount}>Increase</button> </div> ); } });
THAT'S IT
REACT RECAP Everything is a component Components accept props Components
can have a state !
DEMO
THANKS
<Speaker questions={?} />