0
votes

I am using NGRX to store a simple object, but when the object is added to the state tree, the name of the reducer is added too, which generates issues when I strongly type my selector and subscribe to it

The code looks as follow:

Reducer:

  export interface State extends AppState.State {
   player: PlayerState;
  }

 export interface PlayerState {
  currentSkick: ISkick;
  }

 export const initialState: PlayerState = {
 currentSkick: null,
 };

 export const reducer = createReducer<PlayerState>(
  initialState,
  on(
   PlayerActions.setCurrentSkick,
   (state, action): PlayerState => {
    return {
     ...state,
     currentSkick: action.skick,
    };
   }
  )
 );

Selector:

const getCurrentSkickFeaturesState = createFeatureSelector<PlayerState >(
 'player'
);

export const getCurrentSkick = createSelector(
   getCurrentSkickFeaturesState,
   (state) => state.currentSkick
 );

Actions:

export const setCurrentSkick = createAction(
  '[Player] Set Current Skick',
   props<{ skick: ISkick }>()
 );

Feature Module:

@NgModule({

 imports: [
   StoreModule.forFeature('player', { reducer }),
  ],
})
export class SkickPlayerModule {}

App Module:

@NgModule({
declarations: [AppComponent ],
imports: [
BrowserAnimationsModule,
StoreModule.forRoot({}, {}),
StoreDevtoolsModule.instrument({
name: 'player',
maxAge:25,
logOnly: environment.production
})],
})
export class AppModule {}

Skick Interface:

export interface ISkick {
  fileVersion: string;
  formatVersion: string;
   title: string;
   author: string;
   authorEmail: string;
   creationDate: Date;
   uniqueId: string;
   thumbnailLink: string;
   frames: Frame[];
    assets: Assets;
 }

and The Component:

  //Dispatch the action set the current object
  async loadSkick(zip: JSZip) {
    let skickPlayer = await this.getFileToText(zip);  
    this.store.dispatch(PlayerActions.setCurrentSkick({ skick: skickPlayer }));
  }

 ngOnInit(): void {   

 //Subscribe the get the current object
  this.store.select(getCurrentSkick).subscribe((skick) => {
       this.skickPlayer = skick;
     if (this.skickPlayer) this.performFrame(0);
  });

  }

because of the fact ngrx is adding the name of the reducer to the tree as the following image depicts:

enter image description here

When trying to read the object from the subscription it fails because that "reducer" name has been pin in the tree. Any idea why? The structure of the object in the tree should be Player.currentSkick and not player.reducer.currentSkick

1
Can you post the module where you register your reducer with the StoreModule, such as where you call StoreModule.forRoot() or StoreModule.forFeature()?Aaron Adrian
@AaronAdrian just added the modules code. Thanks.D.B
Maybe your ISkick interface contains this reducer key.user1592129
@user1592129 not at all. Just added to the post too.D.B

1 Answers

1
votes

In your modules, if you import the reducer like

import * as fromMyFeature from 'somewhere.reducer';

You should use the import part like this:

@NgModule({

 imports: [
  StoreModule.forFeature('player', fromMyFeature.reducerExportName),
 ],
})
export class SkickPlayerModule {}