Reputation: 199
Hi great internet minds,
I'm trying to understand advanced uses for ngrx selectors and working on digesting this article: ngrx selectors
There are examples showing createSelctors and some of pure rxjs magic. Is any advantage to using one vs another. The method of interests are getAllActiveUsersNGRX VS getAllActiveUsersRXJS.
Sample
const selectUserStore = (state: GlobalState) => state.userStore;
const getAllUsers = createSelector(
selectUserStore ,
(state: UserState) => state.allUsers
);
const getAllActiveUsersNGRX = createSelector(
getAllUsers,
(users: IUser[]) => users.filter(user => (user.active === true))
);
const getAllActiveUsersRXJS = pipe(
select(getAllUsers),
map((users: IUser[]) => users.filter(user=> (user.active === true)))
);
I know using them with the store is going to be different also
E.g
store.pipe(select(getAllActiveUsersNGRX));
VS
store.pipe(select(getAllActiveUsersRXJS));
Aside from the way they are used with the store, are there some benefits I have yet to understand or is it purely coding preference?
Thanks
Upvotes: 3
Views: 1592
Reputation: 3649
From outside they are the same. The difference is how often (users: IUser[]) => users.filter(user => (user.active === true))
function will be called.
As createSelector
create memoized function.
The selector function returned by calling createSelector or createFeatureSelector initially has a memoized value of null. After a selector is invoked the first time its memoized value is stored in memory. If the selector is subsequently invoked with the same arguments it will return the memoized value. If the selector is then invoked with different arguments it will recompute and update its memoized value
Upvotes: 4