My first time using Ngrx, although it is relatively simple, I'm getting an error when dispatching to state. When the user opens the App he needs to enter his name and submit, and the next screen supposed to show the " greeting {{ username }} ". After user submit I'm getting this error --> core.js:6014 ERROR TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
Appreciate your help
import { Action } from '@ngrx/store';
import { UserName, WishListItem } from 'src/model/name.model';
export enum AddToStateActionType{
ADD_NAME = '[ADD_NAME] add name',
ADD_TO_WISHLIST = '[ADD_TO_WISHLIST] add to wishlist'
}
export class AddWelcomeNameAction implements Action {
readonly type = AddToStateActionType.ADD_NAME;
constructor(public payload: UserName){}
}
export class AddToWishList implements Action {
readonly type = AddToStateActionType.ADD_TO_WISHLIST;
constructor(public payload: WishListItem){}
}
export type UserAction = AddWelcomeNameAction | AddToWishList ;
<!--Reducer-->
import {AddToStateActionType, UserAction} from './../action/action';
import { UserName, WishListItem } from 'src/model/name.model';
const initialState: any = {
name: '',
wishlist: []
};
export function reducer(state: any = initialState, action: any) {
switch(action.type) {
case AddToStateActionType.ADD_NAME:
return [...state, action.payload.name];
case AddToStateActionType.ADD_TO_WISHLIST:
return [...state. action.payload.wishlist];
default:
return state;
}
}
<!--AppState-->
import { UserName, WishListItem } from 'src/model/name.model';
export interface AppState {
readonly username: UserName;
readonly wishlist: Array<WishListItem> ;
}
<!--Welcome component-->
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { Store } from '@ngrx/store';
import { AppState } from '../app.state/app.state';
import * as Actions from '../../action/action';
import {Router} from '@angular/router';
import { UserName } from 'src/model/name.model';
@Component({
selector: 'app-welcome',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {
userName: UserName = {name: ''};
profileForm = new FormGroup({
userName: new FormControl(''),
});
constructor(private store: Store<AppState>, private router: Router) { }
ngOnInit() {
}
onSubmit() {
this.store.dispatch(new Actions.AddWelcomeNameAction({name: this.profileForm.value}));
this.router.navigate(['/search']);
}
}
...can you addconsole.log(state)before return in reducer? What can you see? - Ivan