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
Construindo interfaces componentizadas com React
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Sérgio Rafael Siqueira
May 17, 2015
Programming
300
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Construindo interfaces componentizadas com React
Sérgio Rafael Siqueira
May 17, 2015
More Decks by Sérgio Rafael Siqueira
See All by Sérgio Rafael Siqueira
PHP: Uma Jornada Inesperada
sergiors
0
170
Startup's world for devs
sergiors
0
66
Other Decks in Programming
See All in Programming
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
3.9k
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
18k
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
1
270
Google Apps Script で Ruby を動かす
kawahara
0
130
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
150
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
430
「人を評価する AI」の設計と実装
ryoyanara
0
150
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
200
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
0
120
Android CLI
fornewid
0
210
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
560
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
220
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.2k
The SEO identity crisis: Don't let AI make you average
varn
0
530
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
320
How to build a perfect <img>
jonoalderson
1
5.8k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
430
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
400
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
Optimizing for Happiness
mojombo
378
71k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Transcript
Construindo interfaces componentizadas com
CTO e Co-fundador da INBEP, startup com foco em educação.
Sérgio Rafael Siqueira github.com/sergiors sergiorsiqueira.com
[email protected]
React é uma library com foco em interface do usuário
Quem usa
Library Framework
O que é interface do usuário?
• Model • Collection
“ É tudo que o usuário pode interagir
{{ mustache }}
<script id="entry-template" type="text/x-handlebars-template"> <p> {{#if liked}} You like this. Click
to toggle. {{else}} You haven't liked this. Click to toggle. {{/if}} </p> </script>
{{ mustache }} <p> {{#liked}} You like this. Click to
toggle. {{^liked}} You haven't liked this. Click to toggle. {{/liked}} </p>
Template + Javascript = Interface
None
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
http://facebook.github.io/react/docs/component-specs.html#component-specifications { getDefaultProps: function() {}, propTypes: function() {}, mixins: function()
{}, // ... }
“ Legal cara, mas a marcação do Template Engine parece
mais fácil…
JSX
var LikeButton = React.createClass({ getInitialState: function() { return {liked: false}
}, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}> You {text} this. Click to toggle. </div> ); } });
JSX on the fly <script src="node_modules/react/dist/JSXTransformer.js"></script> <script src=“like-button.js" type="text/jsx"></script>
Task runner
E se já tenho dados em uma camada de persistência?
var LikeButton = React.createClass({ getInitialState: function() { return { liked:
this.props.liked || false } }, componentWillMount: function() { Backbone.Events.on('change:anywhere', function(data) { this.setState({ liked: data.liked }); }.bind(this)); }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}>
Métodos de ciclo de vida
Mounting Updating componentWillMount componentDidMount componentWillReceiveProps shouldComponentUpdate componentWillUpdate componentDidUpdate Unmounting componentWillUnmount
http://facebook.github.io/react/docs/component-specs.html#mounting-componentwillmount
var LikeButton = React.createClass({ getInitialState: function() { return {liked: false}
}, componentWillMount: function() { Backbone.Events.on('change:anywhere', function(data) { this.setState({ liked: data.liked }); }.bind(this)); }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}> You '+text+' this. Click to toggle. </div>
Chamado imediatamente antes do render inicial. componentWillMount
var LikeButton = React.createClass({ getInitialState: function() { return {liked: false}
}, componentDidMount: function() { $.ajax('example.php') .done(function(res) { this.setState({ liked: res.liked }); }.bind(this)); }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}> You '+text+' this. Click to toggle.
componentDidMount Chamado imediatamente após o render inicial. A DOM já
esta pronta! Ideal para trabalhar com biblioteca de terceiros que precisam do objeto na DOM.
Performance
Event Delegation
<div id="um"> <div id="dois"> <div id="tres"></div> </div> </div>
http://loopinfinito.com.br/2013/05/14/entendendo-captura-e-propagacao-de-eventos/ .0 -> #um .0.0 -> #dois .0.0.0 -> #tres
None
Virtual DOM
Por que mutação é ruim
None
None
computa a diferença aplica a mudança cria/altera
Dúvidas? discussões apenas apenas no bar!