I have a NGXS state defined as below
export interface Project{
_id:string,
name:string
}
export class ProjectStateModel {
projects: Project[];
}
@State<ProjectStateModel>({
name: 'Projects',
defaults: {
projects: [{_id:'111', name:'Project 1'}, {_id:'222', name: 'Project 2'}]
}
})
@Selector()
static getAll(state: ProjectStateModel) {
return state.projects;
}
@Selector()
static getById(state: ProjectStateModel) {
return (id) => {
return state.projects.find(p => p._id === id);
};
}
@Action(ProjectsAction.Add)
add({ getState, patchState }: StateContext<ProjectStateModel>, { payload }: ProjectsAction.Add)
{
const state = getState();
patchState({
projects: [...state.projects, ...payload]
})
}
Its a pretty straight forward state implementation. The state has 2 projects by default; 2 selectors, one to get all projects and other to get by id and an action to add a new project.
I am using the state in one of the components as shown below
export class ProjectDetailComponent{
selectedPROJ: Project;
selectedPROJ$: Observable<Project>;
constructor(){
this.selectedPROJ$ = this.store.select(ProjectState.getById)
.pipe(map(filterFn => filterFn('111')));
this.test = this.selectedPROJ$.subscribe(p => {
console.log(p); //this get logged every time there is a change in state even though the project 111 hasn't changed
})
}
}
The issue I am facing is every time I update the state(say like add a new project to state), the selector for get by id re-triggers even though the selected item from the state hasn't changed.
For example in the code shown above I have selected project with id 111
. If I dispatch an Add
action to add a new project the selector getById
re-triggers.
Is there any way in NGSX to define a parameterized selector which only gets triggered only if the selected item changes in the state?
I have looked at injectContainerState
in NGXS docs but dont understand how to use that with this parametrized selector.