Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Angular, ngrx data removed when fired dispatch event

I have 2 API calls for data fetching and 2 dispatch events for the data store in ngrx store.

The problem is When I call the first FETCH_FINANCIAL_INSTITUTION dispatcher and 2nd FETCH_USER_GROUPS dispatcher, institutions are removed. See the below screenshots.

1st stage

2nd stage

See below my implementations,

  1. For Institutes,

    this.store.dispatch({
           type: Action.FETCH_FINANCIAL_INSTITUTION,
           payload: <IFinancialInstitution[]> institutions,
    });
    
  2. For user groups,

    this.store.dispatch({
       type: Action.FETCH_USER_GROUPS,
       payload: <IUserGroup[]> userGroups,
    });
    

Here are the reducers,

  1. Institute reducer

     export interface IFinancialInstitutionState {
       institutes: IFinancialInstitution[];
     }
    
     export class FinancialInstitutionState implements 
       IFinancialInstitutionState {
         institutes: IFinancialInstitution[] = [];
     }
    
     export function FinancialInstitutionReducer(
         state: IFinancialInstitutionState = new FinancialInstitutionState(), 
         action) {
              switch (action.type) {
                  case Action.FETCH_FINANCIAL_INSTITUTION:
                      return {...state, ...{institutes : action.payload}};
              }
     }
    
  2. User group reducer

      export interface IUserGroupState {
        userGroup: IUserGroup[];
      }
    
      export class UserGroupState implements IUserGroupState {
            userGroup: IUserGroup[] = [];
      }
    
      export function UserGroupReducer (
            state: IUserGroupState = new UserGroupState(), action,
      ) {
          switch (action.type) {
                  case Action.FETCH_USER_GROUPS :
                  return {...state, ...{userGroup : action.payload}};
        }
      }
    

root reducer,

export const rootReduces = {
  authData : AuthReducer,
  usersData : UsersReducer,
  systemData : SystemReducer,
  institutesData : FinancialInstitutionReducer,
  userGroupData : UserGroupReducer,
};

AppState.ts

export interface AppState {
  readonly authData: IAuthResult[];
  readonly usersData: UserState;
  readonly systemData: SystemConfigs;
  readonly institutesData: IFinancialInstitutionState;
  readonly userGroupData: IUserGroupState;
}
question from:https://stackoverflow.com/questions/65898244/angular-ngrx-data-removed-when-fired-dispatch-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finally, I solved my problem. Ill post it here and it will help someone.

The problem is that in my Reducer function the default in the switch case returning the initialState instead of returning the state. After adding default to the switch case problem solved.

export function FinancialInstitutionReducer(
   state: IFinancialInstitutionState = new FinancialInstitutionState(), action) {
        switch (action.type) {
           case Action.FETCH_FINANCIAL_INSTITUTION:
                  return {...state, ...{institutes : action.payload}};
           default:
                  return state; // this is added line
        }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...