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
SPAの状態管理
Search
yuto hara
October 08, 2018
530
0
Share
SPAの状態管理
社内LT会での資料
yuto hara
October 08, 2018
More Decks by yuto hara
See All by yuto hara
テストカバレッジ100%のプロジェクトで学んだ、 ユニットテストの書き方
hxrxchang
1
2k
componentの分け方
hxrxchang
0
92
インドカレー屋で学ぶ非同期処理
hxrxchang
1
660
Featured
See All Featured
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.4k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
340
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
69
39k
Designing for humans not robots
tammielis
254
26k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
sira's awesome portfolio website redesign presentation
elsirapls
0
220
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
150
How to make the Groovebox
asonas
2
2.1k
The SEO Collaboration Effect
kristinabergwall1
1
420
Six Lessons from altMBA
skipperchong
29
4.2k
Test your architecture with Archunit
thirion
1
2.2k
Transcript
SPAの状態管理について lunch LT
前提知識: component指向 SPAでは、componentとrouterを組み合わせて アプリケーションを構築する。
ルーティング pathが/detail/:post_idのとき、 DetailComponentを表示する
/detail/:post_id 画像の部分は imageViewComponent 詳細の部分は detailInfoComponent 両方を囲っているのが detailComponent
componentの中でcomponentが呼ばれて 入れ子構造になっている 複数のcomponentを組み合わせてページを 構成する
コンポーネント指向での課題 = 状態管理 状態とは フロントエンドアプリケーションが持つデータのこと。 例) - APIで取得するデータ - アプリの状態
- サイドバーが開いてるかどうか - 使用されてるデバイスの種類 などなど
状態管理をしないと。。 1つの値を複数のコンポーネントで使い回したいとき。 - サーバーのデータだったら、それぞれコンポーネントでajaxする? 例) ユーザー情報はあらゆるコンポーネントで使う。 - アプリケーション内だけのデータだったら。。。 整合性が取れなくなる可能性が。。。 状態は1箇所で管理しよう!
Reduxを使う Reduxとは Fluxという概念を拡張した状態管理のライブラリ。 三大原則と処理の流れを覚える。 ngrxとは Angularに特化したRedux。 ルールとか考え方はReduxと一緒。多分。
Reduxの三大原則 1. Single Source of Truth アプリケーションの状態は、アプリケーション内に1つしかない、 Storeと呼ばれるオブジェクトで管理される。 状態の一元管理が可能に!
Chrome拡張の”Redux-DevTools”で ログを出さずに、最新の状態が 確認できる。 airbnb https://www.airbnb.jp/
Reduxの三大原則 2. State is read-only 状態は直接変更できない。 Storeにactionをdispatchして、 ReducerでStateを更新しなくてはならない。 ?????????????????
Reduxの処理の流れを把握する 引用 https://medium.com/default-to-open/understanding-a-large-scale-angular-app-with-ngrx-80f9fc5660cc
Actionをdispatchして、reducerでStateを更 新する。 Actionをdispatchして、reducerでStateを 更新する。 Actionをdispatchして、reducerでStateを 更新する。 復唱して覚えましょう
Reduxの三大原則 3. Mutations are written as pure functions 状態を変更する関数(reducer)は純粋関数でなければならない。 (すみません、よくわかりません。。。)
Reduxでなにが嬉しいのか 状態をstoreで一箇所で管理できる。 componentはview(見た目)の描画と、 actionのdispatchだけ行い、 ビジネスロジックはreducerに責務を分離できる。
store component client side Backend HTTP dispatch action Select
Componentの分け方 Container Component Presentational Component
Container Component Storeから状態をSelectする。 子供の関係にあるPresentational Componentに値を 渡す。 Actionをdispatchする。
Presentational Component 親の関係にあるContainer Componentから 値を受け取り、それを描画するだけ。 ロジックは極力持たない。
両方を囲っているのが detailComponent => Container Component 画像の部分は imageViewComponent => Presentational Component
詳細の部分は detailInfoComponent => Presentational Component
componentを分ける理由 reducer / component の責務をさらに分離。 storeとやりとりするcomponent(container component)と、 viewを表示するcomponent(presentational component)の 責務を分けて、どこでなにをやってるかさらに分かり易くする。
テストを書き易くする
まとめ 復唱しましょう。 Actionをdispatchして、reducerでStateを更新する。 Container ComponentとPresentational Component 概念がわかるとFW問わず読めるようになると思いま す!!多分。。 状態管理とは、状態をstoreで管理する手法のこと。