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
vuexで何をするか、何をしないか / what should do or not with ...
Search
philomagi
December 11, 2019
Programming
4.2k
9
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
vuexで何をするか、何をしないか / what should do or not with Vuex
・vuexの責務は何か
・vuexのアンチパターン考察
・vuexで何をするべきか、何をしないべきか
philomagi
December 11, 2019
More Decks by philomagi
See All by philomagi
SkillとAgentを一元管理する / Centrally manage Skills and Agents
tooppoo
0
49
初めてのGitHub Actions / GitHub Actions at First
tooppoo
0
180
OSS開発を通じて考える、AI時代開発のこれから / The Future of Software Development in the AI Era
tooppoo
0
93
Bunではじめる standalone CLI / start standalone-cli with bun
tooppoo
0
33
吝嗇家のためのAI活用 / AI development for miser - ChatGPT + Issue Driven Development
tooppoo
0
220
Multi-Agent並列開発を 安全に回すための技術 / Technology for Safely Multi-Agent Parallel Development
tooppoo
0
310
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.9k
なぜ定義は問題解決に直結するのか/why-definitions-are-linked-to-problem-solving-with-tdd
tooppoo
0
110
ドメイン駆動設計のホーリズム的側面 / domain-driven-design and holism
tooppoo
0
240
Other Decks in Programming
See All in Programming
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
140
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
150
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
300
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
220
20260828_品質と開発生産性を両立させる、AI時代のE2Eテストの考え方
magicpod
0
200
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
610
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
240
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
3
1.5k
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
150
iOSDC2026登壇資料.pdf
riofujimon
0
120
Jetpack Compose メカニズム
skydoves
0
140
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
240
Featured
See All Featured
HDC tutorial
michielstock
2
860
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
320
Prompt Engineering for Job Search
mfonobong
0
450
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.2k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
500
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
430
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Typedesign – Prime Four
hannesfritz
42
3.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Transcript
Vuexで何をするか、何をしないか vuexのアンチパターン考察 2019/12/11 @Yumemi.vue #yumemi_vue 1
発表者 @Philomagi • 主にフロントエンド主体のWEB系エンジニア • ScalaとTypescriptが好き • PHPは中々縁が切れない悪友 ◦ 最近は、「然程悪いやつでもないな」と思い始めてる
2
概要 1. Vuexとは何か 2. Vuexの責務を考える 3. Vuexアンチパターン私見 4. Vuexですること、しないこと 3
1. Vuexとは何か 4
5 https://vuex.vuejs.org/ja/
6 Vuexとは何か • 状態管理の方法のパターンを提供するライブラリ ◦ Flux・Redux・Elm Architectureから影響(公式) • 単方向データフローを強制する ◦
view → action → mutation → state → getter → view → … • グローバルデータの中央一元管理 ◦ Vuexのstoreは、グローバルかつシングルトン
2. Vuexの責務を考える 7
8 何故Vuexを使うのか • 単方向データフローを強制する • グローバルデータの中央一元管理
9 何故Vuexを使うのか • 単方向データフローを強制するため • グローバルデータのを中央一元管理するため
10 • 単方向データフローを強制するため • グローバルデータのを中央一元管理するため → グローバルデータを 安全に一元管理するため 何故Vuexを使うのか
Vuexの立ち位置 11 Vue Vuex Vuex = Vue(=View)から利用される
Vuexの立ち位置 12 Vue Vuex js/tsモジュール
Vuexの立ち位置 13 Vue Vuex js/tsモジュール Vue以外からは利用されない
Vuexの立ち位置 14 Vue Vuex js/tsモジュール Vuexから利用は有り得る
• 目的 ◦ グローバルデータを安全に一元管理したい • 立ち位置 ◦ Vue(View)からのみ利用される Vuexの責務 15
Vuexの責務 グローバルデータを 安全に一元管理する方法を Vue(View)に提供すること 16
3. Vuexアンチパターン私見 17
Vuexのアンチパターンになり得るもの 18 Vuexの責務を逸脱するもの全て
• 一度に一つのことしかしない 単一責任の原則 19
• 一度に一つのことしかしない • Vuexには、既に一つの責務が有る 単一責任の原則 20
• 一度に一つのことしかしない • Vuexには、既に一つの責務が有る • それ以上のことをVuexにさせない • それ以上のことにVuexを使わない 単一責任の原則 21
Vuexのアンチパターンになり得るもの 22 Vuexの責務を逸脱するもの全て
Vuexアンチパターン(と思っているもの) 23 • emit/propsのショートカットに使う • 全てのデータをvuexに置く • 全てのロジックをvuexに置く
GlobalBusアンチパターン(仮) • 子孫コンポーネントへのデータ伝達にpropsを使わず、子孫 コンポーネントから直接vuexを参照する • 親コンポーネントへのイベント伝達にemitを使わず、 子孫コ ンポーネントから直接vuexのactionを叩く アンチパターン: emit/propsのショートカットに使う
24
25 Vuex Page Component Component Component Page Component Component Component
アンチパターン: emit/propsのショートカットに使う props emit
26 Vuex Page Component Component Component Page Component Component Component
アンチパターン: emit/propsのショートカットに使う props emit
問題点 • 子孫コンポーネントが直接vuexに依存するため ◦ 再利用性が低くなる ◦ テストが難しくなる • Vuexへの依存が複雑になる •
Vuexのリファクタが難しくなる 27 アンチパターン: emit/propsのショートカットに使う
対策 • emit/propsのショートカットにvuexを使わない • 愚直に親→子のデータフローを守る ◦ 面倒でも、vuexへの依存爆発よりはマシ 28 アンチパターン: emit/propsのショートカットに使う(対策)
All In Oneアンチパターン(仮) • クライアント側で利用する全てのデータを、 Vuex のデータとして定義する • コンポーネントではデータを持たず、 全て Vuexのデータを参照する アンチパターン:
全てのデータをvuexに置く 29
Vuex 30 Page Component Component Component Page Component Component Component
アンチパターン: 全てのデータをvuexに置く props emit DataA DataB DataC DataD DataE DataF DataG DataH
Vuex 31 Page Component Component Component Page Component Component Component
アンチパターン: 全てのデータをvuexに置く props emit DataA DataB DataC DataD DataE DataF DataG DataH
Vuex 32 Page Component Component Component Page Component Component Component
アンチパターン: 全てのデータをvuexに置く props emit DataA DataB DataC DataD DataE DataF DataG DataH
Vuex 33 Page Component Component Component Page Component Component Component
アンチパターン: 全てのデータをvuexに置く props emit DataA DataB DataC DataD DataE DataF DataG DataH
問題点 • Vuexが肥大化し、全体像の把握が困難になる • Vuexのリファクタが難しくなる • データのスコープが不明瞭になる 34 アンチパターン: 全てのデータをvuexに置く
対策 • Vuexに入れるべきデータを吟味する ◦ 全体として管理すべきものはVuexへ • コンポーネントに閉じるデータは コンポー ネントで管理する ◦ グローバル変数よりローカル変数
35 アンチパターン: 全てのデータをvuexに置く(対策)
Smart Storeアンチパターン(仮) • クライアント側で利用する全てのロジックを、Vuex のaction/mutation/getterとして定義する • コンポーネントは独自の振る舞いを持たず、 全て Vuexの振る舞いを利用する アンチパターン: 全てのロジックをvuexに置く
36
Vuex 37 Page Component Component Component Page Component Component Component
アンチパターン: 全てのロジックをvuexに置く props emit FuncA FuncB FuncC FuncD FuncE FuncF FuncG FuncH
Vuex 38 Page Component Component Component Page Component Component Component
アンチパターン: 全てのロジックをvuexに置く props emit FuncA FuncB FuncC FuncD FuncE FuncF FuncG FuncH
Vuex 39 Page Component Component Component Page Component Component Component
アンチパターン: 全てのロジックをvuexに置く props emit FuncA FuncB FuncC FuncD FuncE FuncF FuncG FuncH
問題点 • Vuexが肥大化し、全体像の把握が困難になる • Vuexのリファクタが難しくなる • ロジック単体のテストが難しくなる • ロジック単体の再利用が難しい 40
アンチパターン: 全てのロジックをvuexに置く
対策(案) • ロジックはそれ自体独立させる ◦ store非依存でテスト可能 ◦ コンポーネントからもvuex非依存で再利用可能 • Vuexからロジックを呼び出す ◦
actionから呼び出し、その結果を保存する 41 アンチパターン: 全てのロジックをvuexに置く(対策)
Vuex 42 アンチパターン: 全てのロジックをvuexに置く(対策) Page Component Component Component props emit
Domain Action Mutation State Getter Func
4. Vuexですること、しないこと 43
Vuexですること グローバルデータを 安全に一元管理する方法を Vue(View)に提供すること 44
Vuexでしないこと 45 Vuexの責務を逸脱するもの全て
まとめ • Vuexは、安全なグローバルデータ管理の方法を 提供 するライブラリ • 「便利だから」「楽だから」という理由で、 複数の責務 を押しつけない • 単一責任の原則 •
「どこで何をすべきか」を考えることが大事 46
おまけ 47
48 Vuex公式ページより
49 Vuex公式ページより
50 Vuex公式ページより Vuex公式がGlobalBusパターン(仮)を 推奨している疑惑
この発表での見解 • 「Vuex公式が〜と言っている」からといって、それが 「正しい使 い方」「正しい設計」とは限らない ◦ Vuex公式自身が、Vuexの意義や立ち位置を履き違えている可能性もある ◦ あるいは、単にキャッチーな表現として深く考えずチョイスした可能性も有る • ここでは「Vuex公式のこのセンテンスは不適切だ」と、 傲岸不遜
に主張する立場を取ります • 単なる誤読だったらごめんなさい ◦ 一応、英語版も(google翻訳で)同じことを書いているの確認してます 51
ご清聴ありがとうございました 52