Friday, 23 August 2019

Repopulating state in an NgRx Effect

I have JSON that is in my effect that was initially a JSON.stringify(state) and need to add that JSON string back into the state to update the app. (New to Angular and NgRx/Redux).

I have an effect like so (which I'm probably doing wrong):

@Effect({ dispatch: false })
upState$ = this.actions$.pipe(
   ofType(WorkspaceActionTypes.UpState),
   withLatestFrom(this.store.select(fromSpace.getState)),
   tap(([empty, space]) => {
        console.log(JSON.stringify(space));

       var json = "my json file in string form";
       space = json;
   })

);

The json can't go from a string to a type State.

UPDATE:

After looking at the link in the comments, am I supposed to create an effect that calls an action that calls a reducer to update the state? I see this from the link but not sure how to use it yet:

@Effect() 
createArticle$: Observable<Action> = this.actions$
 .ofType<fromActions.CreateAction>(fromActions.CREATE)
 .map(action => action.payload)
 .mergeMap(article => 
     this.articleService.createArticle(article)
     .map(res => new fromActions.CreateSuccessAction(res)) 
     .catch(error => of(new fromActions.CreateFailureAction(error)))
 ); 



from Repopulating state in an NgRx Effect

No comments:

Post a Comment