Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ngrx/store Ignore first emitted value

Tags:

ngrx

store.select() emits previous store state.

Is it possible to subscribe to changes from "this point forward" without getting the previous store value?

like image 421
MichaelAttard Avatar asked Aug 15 '16 10:08

MichaelAttard


People also ask

What is a good use case for NgRx store?

NgRx is a global state management library that helps decouple the Domain and Business layers from the Rendering layer. It's fully reactive. All changes can be listened to using simple Observables, which makes complex business scenarios easier to handle.

What is Memoized selector NgRx?

Because selectors are pure functions, the last result can be returned when the arguments match without reinvoking your selector function. This can provide performance benefits, particularly with selectors that perform expensive computation. This practice is known as memoization.

Is NgRx store persistent?

Currently ngrx/store doesn't support such functionality. But you can maintain state by using a library like ngrx-store-persist to save data to the browsers indexedDB, localStorage or WebStorage. This library automatically saves and restores ngrx/store's data.

Where is NgRx store data is stored?

Where Does NgRx Store Data? NgRx stores the application state in an RxJS observable inside an Angular service called Store. At the same time, this service implements the Observable interface. So, when you subscribe to the store, the service actually forwards the subscription to the underlying observable.


2 Answers

skip operators need piping now, you can use skip like this:

store.pipe(select(...), skip(1));

In terms of the 'hacky' part, it is a standard practice in ngrx to set an initial state with properties set to null. and that value gets emitted initially. so the first value you get will be null in these cases.

Alternatively you could also consider skipwhile(https://www.learnrxjs.io/learn-rxjs/operators/filtering/skipwhile) and use it like this:

store.pipe(select(...), skipWhile(val => val === undefined));

where undefined is the initial value of the property you are interested in. Rather than setting the initial value of the property to undefined, you could use null as the initial value as well, and change the above skipwhile() accordingly.

like image 105
Niz Avatar answered Dec 24 '22 21:12

Niz


If you are not interested in the first emitted value, you should be able to use the skip operator:

store.select(...).skip(1)...
like image 23
cartant Avatar answered Dec 24 '22 21:12

cartant