Slide 1

Slide 1 text

@ngrx/store introduction by example Yannick Baron @yannick_baron Software Architecture Consultant

Slide 2

Slide 2 text

• user interaction 
 clicks, hovers, inputs, ... • data changes 
 receiving data (e.g. http) 
 continuous data updates (e.g. websockets) • state changes 
 form value changes (e.g. checkbox) 
 collapsible component 
 settings changes Reactive Applications react to

Slide 3

Slide 3 text

What/where is state?

Slide 4

Slide 4 text

• router state 
 which route is active 
 route params / query params 
 What/where is state?

Slide 5

Slide 5 text

• router state 
 which route is active 
 route params / query params • application state 
 active user 
 wishlist items / noti fi cations 
 What/where is state?

Slide 6

Slide 6 text

• router state 
 which route is active 
 route params / query params • application state 
 active user 
 wishlist items / noti fi cations • component state 
 game details 
 selected video 
 video playing? 
 What/where is state?

Slide 7

Slide 7 text

• router state 
 which route is active 
 route params / query params • application state 
 active user 
 wishlist items / noti fi cations • component state 
 game details 
 selected video 
 video playing? 
 What/where is state?

Slide 8

Slide 8 text

Handling state is hard

Slide 9

Slide 9 text

• subject in a service • combining streams to deliver view model • subscribing via async pipe • OnPush change detection + immutability ➡ performance boost 
 more control over change detection ➡ no unforeseen side effects 
 by updating state from different sources Common Concepts in advanced reactive Angular

Slide 10

Slide 10 text

• standardised way to handle application state • indirection keeps components clean • Single Responsibility Principle • perfect for if your application is all about state • ideal if several data sources need to be abstracted from 
 (e.g. user change via button click, http request, websocket) • time travel debugging Why @ngrx/store?

Slide 11

Slide 11 text

Introduction to @ngrx/store by example

Slide 12

Slide 12 text

• router state 
 route params / query params • application state 
 list of favorites • component state 
 list of entities 
 current entity to edit An example Project

Slide 13

Slide 13 text

• State (Store) • Selector • Action • Reducer • Effect • @ngrx/component-store @ngrx/store concepts https://ngrx.io/guide/store

Slide 14

Slide 14 text

• State (Store) • Selector • Action • Reducer @ngrx/store concepts https://ngrx.io/guide/store

Slide 15

Slide 15 text

State (Store)

Slide 16

Slide 16 text

• single immutable data structure • de fi nes which data will be held • accessible across the whole app • can be split into feature module states State (Store)

Slide 17

Slide 17 text

• discuss what goes into the store • think about storing data ef fi ciently • avoid nested data • do not blindly put everything into the store • do not put every single property of every component there • do not duplicate state (if you need to store hundreds of entities consider the @ngrx/entity package) State (Store)

Slide 18

Slide 18 text

Selector

Slide 19

Slide 19 text

• reading from the store • pure functions to watch slice of state • combinable • feature selectors to select feature module state • can be used to transform data • use async pipe to subscribe to selects • avoid "synchronous reads" Selector https://ngrx.io/guide/store

Slide 20

Slide 20 text

Action

Slide 21

Slide 21 text

• used to communicate with the store • can be dispatched from anywhere • describe unique events Action https://ngrx.io/guide/store

Slide 22

Slide 22 text

Reducer

Slide 23

Slide 23 text

• responsible to update the state • reacts to action • synchronous, pure functions without side-effects • nested data leads to complex reducer functions • keep reducers simple Reducer

Slide 24

Slide 24 text

• State (Store) • Selector • Action • Reducer Concepts so far https://ngrx.io/guide/store

Slide 25

Slide 25 text

• State (Store) • Selector • Action • Reducer • Effect @ngrx/store concepts https://ngrx.io/guide/store

Slide 26

Slide 26 text

• State (Store) • Selector • Action • Reducer • Effect • @ngrx/component-store @ngrx/store concepts https://ngrx.io/guide/store COMPONENT-STORE

Slide 27

Slide 27 text

Effect

Slide 28

Slide 28 text

COMPONENT-STORE • synchronous and asynchronous side-effects • react to action - dispatch an action (possibly) • use angular services Effect

Slide 29

Slide 29 text

@ngrx/component-store

Slide 30

Slide 30 text

COMPONENT-STORE • store-like implementation on component level • stand-alone package • standardised subject-as-a-service • connect to store via selectors @ngrx/component-store

Slide 31

Slide 31 text

• no nested data • think about what goes into store and how • use component store for local state • do not duplicate state • structure your store into feature module stores • combine selectors + use selectors for transformation • rarely subscribe to selectors • use async pipe + OnPush @ngrx/store advice

Slide 32

Slide 32 text

It's a wrap! @yannick_baron [email protected]