Search code examples
angularreduxngrx

Where to transform store property in Redux/ngrx


I have the following setup

someReducer.ts

export interface State {
   someProp: MyModel;
}
// some action listeners ..
// 
//

export const getProp = (state: State) => state.someProp;

selector.ts

export const getProperty = createSelector(getState, fromSomeReducer.getProperty);

Now say in my components I want to use a transformed version of this someProp like

myProp = this.store.pipe(select(fromSelector.getProperty)).pipe(map(val => //some logic here));

Now this mapping logic can be long, and I dont want to be duplicating this code in several components.

Where does it make sense the most to put this mapping?

I add it to the reducer like this

export const getMapped = (state: State) => state.map(val => // some logic); and then I get this in the selector.

Is this the recommended approach?


Solution

  • If I understand, you are looking for Selector

    export const mySelector = createSelector(
      getProperty,
      val => // Your logic go here
    )
    

    You can now use it

    this.store.pipe(select(fromSelector.mySelector)).subscribe(...);