in ngrx, there are 2 ways(see following code) to parameterize a selector.
Question: what's the difference between them?
const getUser1 = () =>
createSelector(
getEntities,
(state, props) => state[props.id]
);
const getUser2 = id =>
createSelector(
getEntities,
state => state[id]
);
I think memoization works fine in both.
and article below says....
If the parameter doesn’t change over time we can use a factory function https://blog.angularindepth.com/ngrx-parameterized-selector-e3f610529f8
ok. these selectors are used as follows.
@Component({
selector: 'app-user-container',
template: ``
})
export class UserContainerComponent {
user1$ = this.store.pipe(select(getUser1(), { id: 1 }))
user2$ = this.store.pipe(select(getUser2(2)))
constructor(private store: Store<AppState>) {}
}
in both case (getUser1()
and getUser2(2)
), factory functions are evaluated when intializing the class(so, parameter will not be changed for both selector).
I couldn't find the difference in this case....
when is getUser1
useful?
Pre v6.2 it wasn't possible to use the first selector example, so the only way to pass props to a selector was to create a factory function.
Both examples in the question do exactly the same.
The difference would be that props
would be passed down to all selectors in createSelector
, the factory function would not.