Our web app loads values in the NgRx store which are set to null in the initial state. I'm looking for a way to nicely filter non-null values because I keep seeing in components
this.myValue = this.store.pipe(
select(mySelector),
filter(notNull),
);
// where
const notNull = (data) => data !== null;
And I consider this code duplication, it'd be great to handle this at selector level. But I'm also open to other solutions.
One thing I tried is a custom createSelector:
import {createSelector, createSelectorFactory, defaultMemoize} from '@ngrx/store';
const isEqualOrNull = (a: any, b: any): boolean =>
a === b || b === null;
const createSelectorNotNull = createSelectorFactory((projectionFn) =>
defaultMemoize(projectionFn, isEqualOrNull));
Unfortunately, this solution would not handle initial null values, only further ones.
Is there a nice solution to this seemingly frequent problem?