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
0
490
SPAの状態管理
社内LT会での資料
yuto hara
October 08, 2018
Tweet
Share
More Decks by yuto hara
See All by yuto hara
テストカバレッジ100%のプロジェクトで学んだ、 ユニットテストの書き方
hxrxchang
1
1.9k
componentの分け方
hxrxchang
0
83
インドカレー屋で学ぶ非同期処理
hxrxchang
1
620
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.5k
Building Applications with DynamoDB
mza
95
6.5k
Balancing Empowerment & Direction
lara
1
360
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Site-Speed That Sticks
csswizardry
10
660
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.7k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Statistics for Hackers
jakevdp
799
220k
How GitHub (no longer) Works
holman
314
140k
Docker and Python
trallard
44
3.4k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
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で管理する手法のこと。