user3887366
user3887366

Reputation: 2594

Ngrx how to get rid of type in selector

In an effect class I've got this code

 withLatestFrom(this.store.pipe(select(selectSettings))),
 tap(([action, settings]) => {
  console.log('settings', settings); 
  /* 
   print {lang:'en',theme:'dark', type: "[Settings Page] Change Language"}
   instead of simply {lang:'en',theme:'dark'}
 */ 
  this.localStorageService.setItem(SETTINGS_KEY, settings);
 }

I'd like to know, please, how to get rid of it and why there is this extra property, thanks

Upvotes: 0

Views: 413

Answers (1)

mat.hudak
mat.hudak

Reputation: 3193

Check your [Settings Page] Change Language reducer. It looks like it's incorrect and adds its own type to the state

This might be happening

const changeLanguage = createActions('[Settings Page] Change Language', props<{ lang: string }>())

on(changeLanguage, (state, action) => {
  ...state,
  ...action // Action object is { lang: 'eng', type: '[Settings Page] Change Language' }
});

// correct version
on(changeLanguage, (state, { lang }) => {
  ...state,
  lang
  // lang: action.lang - if you don't spread the action object
});

Upvotes: 2

Related Questions