Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Immutable.jsとReact @Wantedly ~入門編~
Search
Kento Moriwaki
April 19, 2016
Programming
76k
8
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Immutable.jsとReact @Wantedly ~入門編~
WantedlyでReactとImmutable.jsをどう使っているか
Kento Moriwaki
April 19, 2016
More Decks by Kento Moriwaki
See All by Kento Moriwaki
わかった気になれる CRDT を使った共同編集
kentomoriwaki
8
5.7k
デザインシステムを導入してUIに秩序を取り戻す - React (Native)編 #rejectron2018
kentomoriwaki
16
4k
ReactでWebとNativeの共通UIライブラリを作ろう
kentomoriwaki
0
1.3k
BFFを導入しなかった理由
kentomoriwaki
4
14k
TypeScript in Wantedly
kentomoriwaki
2
830
5分でわかる React "Suspense"
kentomoriwaki
3
1.5k
導入して1年経ったReact周辺の 技術スタックを反省します | React反省会@Wantedly
kentomoriwaki
10
8.8k
React速習会@Wantedly
kentomoriwaki
1
470
Other Decks in Programming
See All in Programming
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
130
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
310
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
350
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
230
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
340
モジュールの視点からSwiftを読み解く #iosdc
s_shimotori
0
340
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
120
R8 の設定見直しでアプリサイズ削減
andpad
0
140
iOSDC2026登壇資料.pdf
riofujimon
0
210
Ghostty + Neovimで作る 透明でカッコ良い開発環境
j341nono
0
150
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
440
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
480
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
480
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
350
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
4 Signs Your Business is Dying
shpigford
187
23k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
320
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