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
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
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
190
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
130
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
11
4.1k
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
120
Vite+ Unified Toolchain for the Web
naokihaba
0
310
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
200
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
180
Webフレームワークの ベンチマークについて
yusukebe
0
170
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
160
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.1k
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
170
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
260
Featured
See All Featured
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
200
From π to Pie charts
rasagy
0
210
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
140
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.7k
Navigating Weather and Climate Data
rabernat
0
220
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
610
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Site-Speed That Sticks
csswizardry
13
1.2k
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ϥΠϒϥϦͱಉډ͍͠