-
The discussions of moving to factory selectors discuss the case of using static parameters
but not the case when we want to parameterize our selectors based upon an input to the component
When using parameters from an In my use case the component is part of a list and so using some 'current' id selected from the store, or the router doesn't really work. I could build an uber selector at a root level and pass in the fully hydrated object, but that's pretty expensive, and a lot of logic to pull into a single place. It also means I have to be wary of re-rendering a list of items, in which only the content of a single item changed, rather than the list. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I usually change input to be a set/get and handle unidentified in the selector. Would love to hear what others are doing. |
Beta Was this translation helpful? Give feedback.
-
and use |
Beta Was this translation helpful? Give feedback.
-
So to flesh this answer out a bit we end up with components like: class CustomerComponent {
@Input() set customerId(customerId: number) {
if (Number.isInteger(customerId)) {
this.customerId$.next(customerId);
}
};
protected readonly customerId$ = new ReplaySubject<number>(1);
customer$ = this.customerId$.pipe(
switchMap(customerId => this.store.pipe(
select(selectCustomerById(customerId))
)),
shareReplay()
);
constructor(public store: Store) {
}
} Works well with <ng-container *ngIf="customer$ | ngrxPush as customer">
<!-- Do things with customer here -->
</ng-container> |
Beta Was this translation helpful? Give feedback.
and use
this.id$
observable.