I know this has been asked many times before, but i am still not able to understand what is going wrong with my code. It seems like my effects have taken up steroids and are simply not stopping from running endlessly. I am on ngrx/store and ngrx/effects version 10.1.1
Below is the code:
home.effects.ts
public defaultState: DeployedInstanceModel;
@Effect()
loadDeployedInstanceData$ = this.actions$
.pipe(
ofType(DeployedInstancesTypes.GET_TABLE_DATA),
mergeMap(() => {
// reset default state before api call is made
this.defaultState = {
data: {}
};
return this.analyticsService.getDeployedInstanceData().pipe(
map(result => {
this.defaultState.data = result;
console.log('[deployed-instance] the result of api call is -> ', result);
return new DeployedInstanceGetData.GetDeployedInstanceAction(this.defaultState);
}),
catchError(err => {
let errObj = {err: '', url: ''}
errObj.err = err.statusText;
errObj.url = err.url;
console.log('API failure detected on the url -> ', errObj.url);
this.showErrorPopup();
return errObj.err;
})
);
})
)
constructor(
private actions$: Actions,
private analyticsService: AnalyticsService
) { }
and here is how I am trying to dispatch my actions from home component
home.component.ts
ngOnInit(){
this.state = { data: {}};
// first dispatch empty state on page load
this.store.dispatch(new DeployedInstanceGetData.GetDeployedInstanceAction(this.state));
// get the data from the store once api has been called from effects
this.homeDeployedInstanceStore$ = this.store.select('deployedInstanceModel').subscribe(state => {
this.totalData = state.data;
if(Object.keys(this.totalData).length > 0){
console.log(this.totalData);
this.loader.stop();
}else{
this.loader.start();
}
console.log('[Deployed Instance] state from component -> ', this.totalData);
});
}
What I tried:
I tried all the solutions mentioned below:
@Effect({dispatch:false}) - Here data gets extracted once but then my this.store.select is not getting called again when I have the data which returns in empty data shown on home page
take(1) - tried with this solution but then I am not able to call api the second time when user navigates out of the page and comes back again on the same page.
tried removing @Effect but again same problem I am getting.
It would be really helpful if someone can point me in the right direction.

DeployedInstanceGetData.GetDeployedInstanceActionafter effect .. this making a recursion. You should return another action for exampleDeployedInstanceGetData.GetDeployedInstanceSuccessActionwhich will have new effect or do nothing - Franky238