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
NgRx v7
Search
kou
October 19, 2018
Technology
530
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
NgRx v7
NgRxについての紹介とv7の変更点など
kou
October 19, 2018
More Decks by kou
See All by kou
Angular Webアプリケーションの最新設計手法.pdf
koumatsumot0
7
12k
NgRxについて考えたこと
koumatsumot0
0
260
きれいなCSSを書くためのTools
koumatsumot0
0
460
AngularアプリケーションにおけるCSS設計手法
koumatsumot0
8
6k
Other Decks in Technology
See All in Technology
大量データに対しても、生成AIを用いてリーズナブルにデータ加工をしたい!Databricksのai_queryについて調べてみた
kamoshika
1
240
ゴールデンパスは敷いただけでは道にならない ─ 企画部門のエンジニアが技術標準を事業価値に変えるまで
mhrtech
1
240
【公開用】AI_Dev_Ex2026_AI_登壇資料
matsuritechnologies
PRO
1
230
ソニー銀行におけるビジネスアジリティ向上のためのクラウドシフト戦略
srenext
0
960
ファミコンでPHPを動かす / PHP on the Famicom
tomzoh
2
550
”AIを使う” から ”AIに任せる” へ ─ 開発プロセスを再設計してAIを組織標準にするまで
cyberagentdevelopers
PRO
1
100
副作用のある Lambda でも Lambda Power Tuning は使えるのか / lambda-power-tuning-side-effects
koukihosaka
1
130
2026年のソフトウェア開発を考える(2026/07版) / Agentic Software Engineering 2026-07 Findy Edition
twada
PRO
2
180
非定型なドキュメントを効率よくリファクタする 〜えぇ!?仕様書27本の移行が1日で終わったって!?〜
subroh0508
2
590
仕様駆動開発、導入半年。「本当に速くなってるの?」にデータで答える / AICon2026_hirakawa
rakus_dev
0
270
SoccerMaster: A Vision Foundation Model for Soccer Understanding
kzykmyzw
0
150
Oracle Exadata Database Service on Cloud@Customer X11M (ExaDB-C@C) サービス概要
oracle4engineer
PRO
2
8.4k
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
260
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
190
Context Engineering - Making Every Token Count
addyosmani
9
1k
Marketing to machines
jonoalderson
1
5.6k
So, you think you're a good person
axbom
PRO
2
2.1k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
190
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.8k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
How STYLIGHT went responsive
nonsquared
100
6.2k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.4k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Transcript
NgRxの紹介とv7について
@kou bitbank, inc
NgRxとは - “ Reactive libraries for Angular ” - Angularアプリケーションの状態管理ライブラリ
- 似たようなもので NGXS、Akita などがある
Packages @ngrx/store @ngrx/effects @ngrx/router-store @ngrx/store-devtools @ngrx/entity @ngrx/schematics
https://medium.com/default-to-open/understanding-a-large-scale-angular-app-with-ngrx-80f9fc5660cc
個人的にNgRxが良いと思うところ - APIが低レベルで汎用的なので、柔軟性が高い - 開発が安定していて、コード品質も高い - ユーザーが多くて、イベントなどでの発表も多い - RxJSベストプラクティス集なので、 RxJSの秩序が守られる
NgRx簡単年表 1系 2015年12月 @ngrx/storeの公開開始 2系 2016年05月 ngrx/example-appの追加、各パッケージの安定版 4.0.0 2017年07月 @ngrx/entity
の追加、ngrx/platform へのモノリポ移行 5.0.0 2018年01月 @ngrx/schematics の追加 6.0.0 2018年05月 AngularのSemantic Versioningに対応
NgRx v7紹介 - @ngrx/store の新機能の紹介 - @ngrx/entity の新機能の紹介 - v7でのBreaking
Changesの紹介
@ngrx/store 新機能 - Props in Selector (v6.1)
// StateのトップレベルのNamespaceを指定するselector export const selectFeature = createFeatureSelector( 'feature' ); //
上記で取得したStateの中からさらに条件を指定して取得 export const selectCount = createSelector( selectFeature, (stata) => state.counter ); // 使う時 const state = store.select(selectCount) @ngrx/store とは? NgRxの中核であるStoreを提供するモジュール (ReduxのStoreに相当) NgRxではコンポーネントがStoreからStateを取得する際には selectorという機能が使われる (selectorはSQLのようなもの)
// 引数の最後 (ここではid) がpropsとなっている export const selectUserById = createSelector( selectUsers,
(users, id) => users.find(user => user.id === 100) ); Props in Selector 実際にSelectorを使用する時にpropsを渡して、その結果を使用してStateを取得 することが可能になる // 6.0: 取得した結果から抽出する必要があった store.select(selectUsers).pipe( map(users => users.find(user => user.id === 100)) ); // 6.1: selector側で処理できるようになった store.select(selectUserById(100));
@ngrx/entity 新機能 - updateMany by predicate - removeMany by predicate
function reducer(state = initialState, action: Actions): State { switch (action.type)
{ case ActionTypes.ADD_USER: { return adapter.addOne(action.payload.user, state); } case ActionTypes.ADD_USERS: { return adapter.addMany(action.payload.users, state); } case ActionTypes.UPDATE_USER: { return adapter.updateOne(action.payload.user, state); } case ActionTypes.UPDATE_USERS: { return adapter.updateMany(action.payload.users, state); } case ActionTypes.DELETE_USER: { return adapter.removeOne(action.payload.id, state); } case ActionTypes.DELETE_USERS: { return adapter.removeMany(action.payload.ids, state); } // ... } } @ngrx/entity とは? Storeの管理でありがちなEntity操作を提供してくれるモジュール 基本的なCRUD操作をライブラリで提供してくれる
function reducer(state = initialState, action: Actions): State { switch (action.type)
{ case ActionTypes.MARK_AS_READ: { // 保持するEntity全てに変更を適用 return adapter.map((data) => { return data.isRead === false ? { ...data, isRead: true } : data; }, state); } } } updateMany by predicate 保持している全Entityに対してmap関数で指定したアップデートを行う 下記の例では、未読(isRead === false)のデータを全て既読に変更している v7からは指定の条件での一括更新を行うことができる
// v7 function reducer(state = initialState, action: Actions): State {
switch (action.type) { case ActionTypes.DELETE_READ: { return adapter.removeMany(data => data.isRead, state); } } } removeMany by predicate 保持している全Entityに対して指定の条件に合致するものを削除する 下記の例では、既読(isRead === true)のデータを全て削除している 今まではidの配列で削除しなければならなかった // v6以前: 複数削除はIDの配列指定が必要だった function reducer(state = initialState, action: Actions): State { switch (action.type) { case ActionTypes.DELETE_READ: { return adapter.removeMany([1, 2, 3], state); } } }
v7 Breaking Changes - Effects: Actions.ofTypeの削除 - Store: update-reducersのActionのdispatch回数変更 -
RouterStore: デフォルトstate-key名の変更 - RouterStore: Navigation系のActionの変更
// Error @Effect() effect$ = this.actions$ .ofType(UserActions.LOGIN) .pipe(map(() => new
AnotherAction())); // OK @Effect() effect$ = this.actions$.pipe( ofType(UserActions.LOGIN), map(() => new AnotherAction()), ); Effects: Actions.ofTypeの削除 非推奨になっていたActions classのstaticメソッドが削除される予定 これからはRxJSのOperaterとしてのofTypeを使う
// v6: 取付/取外した数の分のActionが発行されていた {type: '@ngrx/store/update-reducers', feature: 'feat1'} {type: '@ngrx/store/update-reducers', feature:
'feat2'} // v7: Actionは1回になり、配列で渡るようになった {type: '@ngrx/store/update-reducers', features: ['feat1', 'feat2']} Store: update-reducersのActionのdispatch回数変更 Storeに対してReducerを取付/取外したときに発行されるActionの仕様変更 (普通は使わないが、store-devtoolsの実装に影響を与えたりしている)
// v6 { routerReducer: RouterReducerState } // v7 { router:
RouterReducerState } RouterStore: デフォルトstate-key名の変更 RouterStoreModuleがRouterのデータをStateオブジェクトに追加する時の key名が変更された ("routerReducer" から "router") // おまけ: state-keyは自分で指定することもできる。v7からはselectorも使えるようになった StoreRouterConnectingModule.forRoot({ stateKey: 'my-custom-key', }) StoreRouterConnectingModule.forRoot({ stateKey: (state) => state['my-key'], })
RouterStore: Navigation系のActionの変更 Routerのイベントによって発行されるActionが新規で2つ追加されて、 発行されるタイミングなどが少し変わった (追加前は3種類、追加後は5種類) この変更によってより柔軟にRouterを扱えるようになった ROUTER_NAVIGATION: ROUTER_CANCEL: ROUTER_ERROR: v6
RoutesRecognized NavigationCancel NavigationError ROUTER_REQUEST: ROUTER_NAVIGATION: ROUTER_NAVIGATED: ROUTER_CANCEL: ROUTER_ERROR: v7 NavigationStart RoutesRecognized NavigationEnd NavigationCancel NavigationError
その他 NgRx 最近のトピック - リポジトリ構成の変更 - Good Action Hygiene の適用
- ngrx/platform - modules/ - effects/ - entity/ - router-store/
- schematics/ - schematics-core/ - store/ - store-devtools/ - example-app/ - example-app-e2e/ ngrx/platform リポジトリ構成の変更 ngrx.ioの導入に伴い、projectsというディレクトリが追加されて、example-app などはそちらで管理されるようになった - ngrx/platform - modules/ - effects/ - entity/ - router-store/ - schematics/ - schematics-core/ - store/ - store-devtools/ - projects/ - example-app/ - example-app-e2e/ - ngrx.io/
Good Action Hygiene の適用 example-app のプロジェクトがGood Action Hygiene対応された。 Good Action
Hygieneとは? ng-conf2018でMike Ryanさんが発表したアクションのカテゴライズと命名方法。これを 適用することで、アクションはより的確にアプリケーションの仕様を表現することができ る。
https://www.youtube.com/watch?v=JmnsEvoy-gY
https://www.youtube.com/watch?v=JmnsEvoy-gY
ngrx.io - angular.ioと同じ設計のドキュメントサイト - masterにマージ済み、近日リリース予定
None
おわりに - Good Action Hygeineなどのベストプラクティスも出てきて、 NgRx界隈は徐々に盛り上がっている - 本家Angular、Materialに比べると開発にそれほど勢いはな いが、着実に良くなっていっている -
v7ではさらにユーザ数が増えることを期待