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がなぜ素晴らしいのか
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Yuichiro MASUI
March 18, 2015
Programming
20k
44
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Reactがなぜ素晴らしいのか
詳しくはこちらへ →
https://www.facebook.com/groups/toretatech/
Yuichiro MASUI
March 18, 2015
More Decks by Yuichiro MASUI
See All by Yuichiro MASUI
Coding Agent用チケットシステム - ticket.sh
masuidrive
0
53
Vue/Nuxt tutorial for Sendai
masuidrive
0
220
ハッカソンで 作ったアプリを売却した話
masuidrive
0
700
DJ hands on for IT Engineers
masuidrive
1
210
自分の棚卸をして レジュメを書こう
masuidrive
5
2k
0から1ヶ月で DJになるまで
masuidrive
4
1.6k
Google Apps Scriptでbotを作る話
masuidrive
1
1.8k
Rails loves React
masuidrive
1
540
トレタの存在理由
masuidrive
0
250
Other Decks in Programming
See All in Programming
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
210
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
500
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
110
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
560
ふつうのFeature Flag実践入門
irof
7
3.7k
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
3.6k
Contextとはなにか
chiroruxx
0
290
CSC307 Lecture 17
javiergs
PRO
0
320
dRuby over BLE
makicamel
2
330
さぁV100、メモリをお食べ・・・
nilpe
0
140
スマートグラスで並列バイブコーディング
hyshu
0
120
3Dシーンの圧縮
fadis
1
690
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
463
34k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
71
40k
The browser strikes back
jonoalderson
0
1.2k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
250
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
190
Chasing Engaging Ingredients in Design
codingconduct
0
220
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2k
Rails Girls Zürich Keynote
gr2m
96
14k
sira's awesome portfolio website redesign presentation
elsirapls
0
280
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
480
Information Architects: The Missing Link in Design Systems
soysaucechin
0
970
Transcript
React.js͕ ͳͥૉΒ͍͠ͷ͔ Yuichiro MASUI <
[email protected]
> Toreta, Inc CTO
DOMૢ࡞ͳ͍ͥ͠ͷ͔ <div id=“item-1”> <span id=“item-1-text” class=“hidden”>foo</span> </div> ͜ͷλάݟ͑ͯΔʁ <button id=“add-item”>Ճ</a>
Ͳ͜Ͱॲཧ͢Δ͔ ͔Βͳ͍ <div id=“item-1”> <span id=“item-1-text”>foo</span> </div> ͍ͭͲ͜Ͱॻ͖͑Δͷ͔ ͔Βͳ͍
DOMૢ࡞ͳ͍ͥ͠ͷ͔ <div id=“item-1”> <span id=“item-1-text” class=“hidden”>foo</span> </div> ͜ͷλάݟ͑ͯΔʁ <button id=“add-item”>Ճ</a>
Ͳ͜Ͱॲཧ͢Δ͔ ͔Βͳ͍ <div id=“item-1”> <span id=“item-1-text”>foo</span> </div> ͍ͭͲ͜Ͱॻ͖͑Δͷ͔ ͔Βͳ͍ DOMͲ͔͜ΒͰมߋՄೳ DOM͕ঢ়ଶΛ͍࣋ͬͯΔ DOMͱίʔυ͕Ε͗͢
ͬͱ؆୯ʹΖ͏ DOMΛॻ͖͑ͳ͍ͰৗʹHTMLશମΛੜ EventListenerΛΊͯonXxxϓϩύςΟΛ͏ HTMLͱJavascriptΛҰͭʹ·ͱΊΔ
ΫϥΠΞϯτϩδοΫ αʔόαΠυੜ ϒϥβ ϩδοΫ (ίϯτϩʔϥ) ϦΫΤετ αʔό ΫϥΠΞϯτ HTMLੜ෦ (render)
HTMLੜ෦ (ςϯϓϨʔτ) ը໘Λදࣔ͢ΔͨΊͷঢ়ଶ (setState) ը໘Λදࣔ͢ΔͨΊͷঢ়ଶ (ςϯϓϨʔτม)
ΫϥΠΞϯτϩδοΫ ϒϥβ αʔό ΫϥΠΞϯτ Πϕϯτ (onSubmit) React.js αʔόϩδοΫ HTMLੜ෦ (render)
ը໘Λදࣔ͢ΔͨΊͷঢ়ଶ (setState)
Example: Todo list http://codepen.io/masuidrive
P.1 - ྻͷΞΠςϜΛදࣔ
/** @jsx React.DOM */ App = React.createClass({ getInitialState: function() {
return { items: [ { text: "Foo" }, { text: "Bar" }, { text: "Buzz" } ] }; }, render: function() { return <div> <div> {this.state.items.map(function(item) { return <div className="item">{item.text}</div>; })} </div> </div>; } }); React.render(<App/>, document.body); P.1 - ྻͷΞΠςϜΛදࣔ
P.2 - ΞΠςϜΛՃ
/** @jsx React.DOM */ App = React.createClass({ getInitialState: function() {
return { newText: "", items: [ { text: "Foo" }, { text: "Bar" }, { text: "Buzz" } ] }; }, addItem: function(event) { event.preventDefault(); this.setState({ items: [{text : this.state.newText}].concat(this.state.items), newText: "" }) }, updateNewText: function(event) { this.setState({ newText : event.target.value }); }, render: function() { return <div> <form onSubmit={this.addItem}> <input value={this.state.newText} onChange={this.updateNewText} /> <button>Ճ</button> </form> ɹ <div> {this.state.items.map(function(item) { return <div className="item">{item.text}</div>; })} </div> </div>; } }); React.render(<App/>, document.body); P.2 - ΞΠςϜΛՃ
addItem: function(event) { event.preventDefault(); this.setState({ items: [{text : this.state.newText}].concat(this.state.items), newText:
"" }) }, updateNewText: function(event) { this.setState({ newText : event.target.value }); }, render: function() { return <div> <form onSubmit={this.addItem}> <input value={this.state.newText} onChange={this.updateNewText} /> <button>Ճ</button> </form> ɹ <div> {this.state.items.map(function(item) { ← 1จࣈೖྗͷͨͼʹݺΕΔ P.2 - ΞΠςϜΛՃ
P.3 - ݅දࣔ ݅ →
P.3 - ݅දࣔ /** @jsx React.DOM */ App = React.createClass({
getInitialState: function() { return { newText: "", items: [ { text: "Foo" }, { text: "Bar" }, { text: "Buzz" } ] }; }, addItem: function() { event.preventDefault(); this.setState({ items: [{text : this.state.newText}].concat(this.state.items), newText: "" }) }, updateNewText: function(ev) { this.setState({ newText : ev.target.value }); }, render: function() { return <div> <form onSubmit={this.addItem}> <input value={this.state.newText} onChange={this.updateNewText} /> <button>Ճ</button> </form> <div>{this.state.items.length}݅</div> <div> {this.state.items.map(function(item) { return <div className="item">{item.text}</div>; })} </div> </div>; } }); React.render(<App/>, document.body);
P.3 - ݅දࣔ }); }, render: function() { return <div>
<form onSubmit={this.addItem}> <input value={this.state.newText} onChange={this.updateNewText} /> <button>Ճ</button> </form> <div>{this.state.items.length}݅</div> <div> {this.state.items.map(function(item) { return <div className="item">{item.text}</div>; })} </div> </div>; } }); React.render(<App/>, document.body);
HTMLΛৗʹ࠶ߏங͢Δ͜ͱͰ ঢ়ଶม ϩδοΫ มΛߋ৽ Ϗϡʔ ม͔Βੜ
Ͱ… • HTMLશମॻ͖͑Δͱ͘ͳ͍ʁ • CSSΞχϝʔγϣϯ͑ͳ͍ΑͶʁ • ࣮ຊޠೖྗͰ͖ͳ͍ΑͶ
<div> <div>3݅</div> <div className=“items”> <div className=“item”> Foo </div> <div className=“item”>
Bar </div> <div className=“item”> Buzz </div> </div> <div> ݱࡏͷDOM ࣮ࡍͷߋ৽ VirtualDOM BrowserDOM ৽͍͠DOM <div> <div>4݅</div> <div className=“item”> <div className=“item”> Foo </div> <div className=“item”> Bar </div> <div className=“item”> Buzz </div> <div className=“item”> One </div> </div> <div> (ͨͩͷJS Object) <div> <div>4݅</div> <div class=“item”> <div class=“item”> Foo </div> <div class=“item”> Bar </div> <div class=“item”> Buzz </div> <div class=“item”> One </div> </div> <div>
<div> <div>3݅</div> <div className=“items”> <div className=“item”> Foo </div> <div className=“item”>
Bar </div> <div className=“item”> Buzz </div> </div> <div> ݱࡏͷDOM ࣮ࡍͷߋ৽ VirtualDOM BrowserDOM ৽͍͠DOM <div> <div>4݅</div> <div className=“item”> <div className=“item”> Foo </div> <div className=“item”> Bar </div> <div className=“item”> Buzz </div> <div className=“item”> One </div> </div> <div> (ͨͩͷJS Object) <div> <div>4݅</div> <div class=“item”> <div class=“item”> Foo </div> <div class=“item”> Bar </div> <div class=“item”> Buzz </div> <div class=“item”> One </div> </div> <div>
ͬͱٕज़తʹ • key • component • property • dangerouslySetInnerHTML •
ৄ͘͠άά͍ͬͯͩ͘͞
͞Βʹ • ComponentͰ࠶ར༻Մೳͳ෦Խ • αʔόαΠυϨϯμϦϯά • ͬͱେ͖ͳΞϓϦͰFlux • iOS/AndroidΞϓϦReactNative
• ίʔυΛॻ͘ਓ͕HTMLॻ͘ • طଘͷJS෦͕͑ͳ͍(͜ͱͳ͍) • ಛʹTwitter Widgetͱ͔֎෦JSۤख
Flux • ߟ͑ํͷ໊લ (MVCͱ͔ͱҰॹ) • ReactͰData͑͞มߋ͢Εը໘ʹө͞ΕΔ • Ϣʔβ͔ΒͷActionΛݩʹDispatcher͕Storeͷ มߋΛݺͼग़͢
Dispatcher Action Store React User action
Dispatcher Action Store React User action render: function() { return
<header id="header"> <h1>todos</h1> <TodoTextInput id="new-todo" placeholder="What needs to be done?" onSave={this._onSave} /> </header>; }, _onSave: function(text) { if(text.trim()) { TodoActions.create(text) } } components/Header.js
Dispatcher Action Store React User action actions/TodoActions.js TodoActions = {
create: function(text) { AppDispatcher.dispatch({ actionType: TodoConst.TODO_CREATE, text: text }); } }
Dispatcher Action Store React User action stores/TodoStore.js AppDispatcher.register(function(action) { switch(action.actionType)
{ case TodoConst.TODO_CREATE: create(action.text); TodoStore.emit(“change”); } }); function create() { var id = Math.random(); _todos[id] = { id: id complete: false text: text }; }
Dispatcher Action Store React User action components/TodoApp.js var TodoApp =
React.createClass({ getInitialState: function() { return({ todos: TodoStore.getAll() }); }, componentDidMount: function() { TodoStore.addListener(this._onChange); }, _onChange: function() { this.setState({todos: TodoStore.getAll()}); }, render: function() { return ... } });
·ͱΊ • αʔόαΠυͷ༷ͳγϯϓϧͳྲྀΕͰAjaxΛ • ը໘Λॻ͖͑Δͱ͖ৗʹશ෦ॻ͖͑ • طଘͷJS UIϥΠϒϥϦͱಉډ͍͠