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
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
54
Vue/Nuxt tutorial for Sendai
masuidrive
0
220
ハッカソンで 作ったアプリを売却した話
masuidrive
0
700
DJ hands on for IT Engineers
masuidrive
1
220
自分の棚卸をして レジュメを書こう
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
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.7k
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
100
Oxcを導入して開発体験が向上した話
yug1224
4
310
OSもどきOS
arkw
0
570
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
690
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
180
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
190
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
250
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
240
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
790
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
200
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
560
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
160
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
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ϥΠϒϥϦͱಉډ͍͠