State management can get really complex for scaled Angular applications. And, NgRx can be an answer for the complexity of this problem. This talk will cover some of the best practices of building an NgRx Angular app. We'll go through building a good action hygiene, efficiently using schematics, improving your apps performance with one-way data binding and memoized selectors, reducing the boilerplate with entities/schematics and introducing ngrx-data. After this session, you'll have more control on the NgRx platform with the grasp of entities, memoized selectors, effects and schematics.
2. @onderceylan
Sharing knowledge on #javascript, #typescript, #angular, #ionic and #pwa
JS Tech Lead @LINKIT, @KLM
Speaker, co-organiser @ITNEXT, @GDG-NL
Ö N D E R C E Y L A N
3. @onderceylan
N G R X P L AT F O R M
• @ngrx/store
• @ngrx/effects
• @ngrx/router-store
• @ngrx/store-devtools
• @ngrx/entity
• @ngrx/data
• @ngrx/schematics
7. @onderceylan
AV O I D G E N E R I C A C T I O N T Y P E S
@ngrx/store/init
[User] Authenticate
[User] Authenticate Success
[WontonSoup] Add One
[WontonSoup] Add One
[Cart] Checkout
[Cart] Checkout Success
@ngrx/store/init
[Login Page] Login
[Auth API] Login Success
[Menu Page] Add Wonton Soup
[Soup Details Page] Add Wonton Soup
[Cart Dropdown] Checkout
[Cart API] Checkout Success
Good Action Hygiene with NgRx @ng-conf - Mike Ryan
8. @onderceylan
AV O I D G E N E R I C A C T I O N T Y P E S
[Login Page] Login
[Menu Page] Add Wonton
[Menu Page] Add Teriyaki
[Menu Page] Add Noodle
[Cart] Checkout
[Auth API] Login Success
[Auth API] Login Failure
[Cart API] Checkout Success
[Cart API] Checkout Failure
[WS API] Update Availability
[WebSocket] Open
[WebSocket] Disconnected
[IndexDB] Save Success
Good Action Hygiene with NgRx @ng-conf - Mike Ryan
17. @onderceylan
– N G R X D O C S
“Because selectors are pure functions, the last result can be returned when the
arguments match without re-invoking your selector function. This can provide
performance benefits, particularly with selectors that perform expensive
computation. This practice is known as memoization.”
22. @onderceylan
S E L E C T O R TA K E A WAY S
• Don’t pollute the store state with derived state, use
state selectors to create view models
• Keep selectors small so they can be used to compose
bigger and more complex selectors
• Take advantage of memoization by using selectors and
customMemoize / resultMemoize when needed
43. @onderceylan
W H AT C A N G O W R O N G
W I T H C O D E G E N E R AT I O N ?
44. @onderceylan
@ N G R X / D ATA
• A single objective: to simplify management of entity
data
• Abstraction of reducers, actions, effects, selectors and
HTTP data services
• Almost anything is customizable at a single entity-type
level
• NgRx Data library is good for querying, caching, and
saving entity data
55. @onderceylan
M A I N TA K E A WAY S
• Maintain a good action hygiene
• Use @ngrx/schematics
• Use selectors
• Attention on serialisation & hydration
• Manage your data with @ngrx/entity or @ngrx/data
• Adjust your views with async pipe and ChangeDetectionStrategy.OnPush
• Use optimistic updates when needed
• Use facades where needed and start using creators to reduce boilerplate
56. @onderceylan
R E S O U R C E S
• https://ngrx.io
• https://github.com/ngrx/platform/tree/master/projects/example-
app
• https://github.com/nrwl/nx/blob/master/packages/angular/src/
runtime/nx/data-persistence.ts
• https://medium.com/ngrx/
• https://blog.angularindepth.com/tagged/ngrx
• https://blog.nrwl.io/search?q=ngrx