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
96
インドカレー屋で学ぶ非同期処理
hxrxchang
1
660
Featured
See All Featured
BBQ
matthewcrist
89
10k
Producing Creativity
orderedlist
PRO
348
40k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.2k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
450
Exploring anti-patterns in Rails
aemeredith
3
460
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Ethics towards AI in product and experience design
skipperchong
2
340
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Scaling GitHub
holman
464
140k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.7k
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で管理する手法のこと。