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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
yuto hara
October 08, 2018
540
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SPAの状態管理
社内LT会での資料
yuto hara
October 08, 2018
More Decks by yuto hara
See All by yuto hara
テストカバレッジ100%のプロジェクトで学んだ、 ユニットテストの書き方
hxrxchang
1
2k
componentの分け方
hxrxchang
0
95
インドカレー屋で学ぶ非同期処理
hxrxchang
1
660
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
6.2k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Evolving SEO for Evolving Search Engines
ryanjones
0
230
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
180
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Skip the Path - Find Your Career Trail
mkilby
1
160
Deep Space Network (abreviated)
tonyrice
0
220
Agile that works and the tools we love
rasmusluckow
331
22k
ラッコキーワード サービス紹介資料
rakko
1
3.8M
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
150
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
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で管理する手法のこと。