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
Immutable.jsとReact @Wantedly ~入門編~
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Kento Moriwaki
April 19, 2016
Programming
8
75k
Immutable.jsとReact @Wantedly ~入門編~
WantedlyでReactとImmutable.jsをどう使っているか
Kento Moriwaki
April 19, 2016
Tweet
Share
More Decks by Kento Moriwaki
See All by Kento Moriwaki
わかった気になれる CRDT を使った共同編集
kentomoriwaki
8
5.2k
デザインシステムを導入してUIに秩序を取り戻す - React (Native)編 #rejectron2018
kentomoriwaki
16
3.9k
ReactでWebとNativeの共通UIライブラリを作ろう
kentomoriwaki
0
1.3k
BFFを導入しなかった理由
kentomoriwaki
4
13k
TypeScript in Wantedly
kentomoriwaki
2
780
5分でわかる React "Suspense"
kentomoriwaki
3
1.5k
導入して1年経ったReact周辺の 技術スタックを反省します | React反省会@Wantedly
kentomoriwaki
10
8.8k
React速習会@Wantedly
kentomoriwaki
1
450
Other Decks in Programming
See All in Programming
朝日新聞のデジタル版を支えるGoバックエンド ー価値ある情報をいち早く確実にお届けするために
junkiishida
1
370
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
1.6k
NOT A HOTEL - 建築や人と融合し、自由を創り出すソフトウェア
not_a_hokuts
2
570
Head of Engineeringが現場で回した生産性向上施策 2025→2026
gessy0129
0
210
AHC061解説
shun_pi
0
320
CDIの誤解しがちな仕様とその対処TIPS
futokiyo
0
170
Ruby x Terminal
a_matsuda
7
580
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
550
AI活用のコスパを最大化する方法
ochtum
0
120
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
210
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
550
CSC307 Lecture 14
javiergs
PRO
0
450
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
75
11k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
370
SEO for Brand Visibility & Recognition
aleyda
0
4.3k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.3k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
290
Building AI with AI
inesmontani
PRO
1
760
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.1k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
260
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
0
2.4k
Transcript
Immutable.jsとReact @Wantedly 入門編 Kento Moriwaki / 森脇健斗
シゴトでココロオドル • 森脇健斗 • Wantedlyの新卒二年目エンジニア • フロントエンド好き – Angular書いてた •
最近、Wantedlyのフロントエンド開発環 境を刷新した – そのとき導入したImmutable.jsについて話し ます 自己紹介
シゴトでココロオドル • Facebookが開発している不変オブジェクト を扱うライブラリ Immutable.jsとは https://facebook.github.io/immutable-‐js/
シゴトでココロオドル • オブジェクトのプロパティに代入する代わりに、 新しいオブジェクトを返す Immutable import { Map } from
'immutable' let map1 = Map({ x: 1, y: 2 }) let map2 = map1.set('x', 2) assert(map1.get('x') === 1) assert(map2.get('x') === 2) assert(map1 !== map2)
シゴトでココロオドル • 便利なメソッド、効率的な変更、深いオブジェ クトも操作しやすい 基本的な挙動 let map1 = Map({ a:
1, b: 2 }) let map2 = map1.merge({ b: 3, c: 4 }) alert(map2.get('b') == 3) // true // 変更なければ同じオブジェクトを返す let map3 = map1.set('a', 1) alert(map3 === map1) // true // 階層深い変更も簡単 let map3 = Immutable.fromJS({ a: { b: { c: 1 } } }) let map4 = map3.setIn(['a', 'b', 'c'], 2) alert(map4.getIn(['a', 'b', 'c']) === 2) // true
シゴトでココロオドル • List • Set • OrderedMap – IDで検索する機会の多いリストに便利 •
OrderedSet • Record – Immutableなモデルクラス作れる (後述) 便利な型
シゴトでココロオドル • ReactとImmutable.jsは相性いい • 主なメリットは、 1. パフォーマンス向上 2. ステートの更新が簡単に書ける 3.
モデルクラス作れる Reactと組み合わせるメリット
シゴトでココロオドル • shouldComponentUpdate – Reactのパフォーマンス向上の基本 – 再レンダーが必要かどうか返す – 自分や親コンポーネントのstateやpropsが変 わった時に呼ばれる
– デフォルトは常にtrue パフォーマンス向上 shouldComponentUpdate(nextProps, nextState) { return compareSomehow(this, nextProps, nextState) }
シゴトでココロオドル • shouldComponentUpdate – 簡単に • コンポーネント毎に複雑な処理を書きたくない – 高速に •
再レンダーするより変更チェックが速くあるべき – 正確に • 間違っていると、変更が反映されないようになる パフォーマンス向上 shouldComponentUpdate(nextProps, nextState) { return this.props.foo !== nextProps.foo }
シゴトでココロオドル • 実際に変更されたかチェックする – 変更がない場合は、同じオブジェクトにしたい • Object.assign などが必要 – 変更ないオブジェクトもコピーされる
Immutable.jsなしの場合 onChange(obj) { let newMap = this.state.map if (newMap.x !== obj.x) { newMap = Object.assign({}, newMap, { x: obj.x }) } this.setState({ map: newMap }) }
シゴトでココロオドル • 変更がなければ、自動的に同じオブジェクト を返す • 必要最小限だけ変更される – 変更がないオブジェクトはコピーされない – Object.assignより効率的
– 簡潔に書ける Immutable.jsを使用すれば onChange(obj) { let newMap = this.state.map.set('x', obj.x) this.setState({ map: newMap }) }
シゴトでココロオドル • Reactのpropsを明確化したい – どんなプロパティを期待しているか不明 – 型が指定できれば嬉しい • モデルにメソッド持たせたい 2.
モデルクラスが作れる TickerComponent.propTypes = { ticket: PropTypes.object.isRequired, } TickerComponent.propTypes = { ticket: PropTypes.instanceOf(Ticket).isRequired, }
シゴトでココロオドル • 型を定義できる – フィールドを定義したクラスを作れる • メソッドを定義できる – 継承して、メソッドを持たせることができる –
複雑なフィールド更新をモデルに持たせる • これも非破壊操作になる Immutable.Record
シゴトでココロオドル lib/immutable/Ticket.js const _Ticket = Record({ id: null, title: '',
assignee: new User(), }) export default class Ticket extends _Ticket { static fromJS(ticket = {}) { return (new this).merge({ id: parseInt(ticket.number), title: ticket.title, assignee: User.fromJS(ticket.assignee), }) } isDarkColor() { return parseInt(this.colorCode.substring(1, 7), 16) > 8388607) } }
シゴトでココロオドル • Immutable.js使えば – Reactのパフォーマンスが簡単に向上できる – ステートの更新が簡潔に書ける – Reactで使いやすい独自モデルクラスが定義で きる
まとめ
シゴトでココロオドル 最後に
シゴトでココロオドル • 一緒に働いてくれる仲間を募集しています。 一緒にReact書きませんか? https://www.wantedly.com/projects/51709