Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 3 ngrx/store reducers not receiving dispatched actions only in production mode

I have an Ionic 3 App that uses web sockets and redux using ngrx/store. At first all of my code is working fine in development mode. In both browsers and real device.

But when I try to built it in production mode. The action is still dispatched but the reducers didn't receive the action that has been dispatched causing that the state of the application not being updated.

Here is my code below of my reducers.

import { Action } from '@ngrx/store';

const UPDATE_AVATAR = '[Websokcet] New ORDER';
type Type = UpdateAvatar

export class UpdateAvatar implements Action {
  readonly type = UPDATE_AVATAR;
  constructor(public payload: any) { }
}

export function UpdateAvatarReducer(state: any, action: Type) {
  console.log('ACTION RECEIVED:', state, action)
  switch (action.type) {
    case UPDATE_AVATAR:
      return action.payload;
  }
}

and in my rootReducers

import { UpdateAvatar, UpdateAvatarReducer } from './reducers/uploadAvatar';

export function rootReducer () {
  return {
    reducers: {
      driverUpdateProfile: DriverUpdateProfileReducer,
    },
  }
}

and in my app.module.ts

import { rootReducer } from '../store/websocket';

// and in the **imports arrays**

StoreModule.forRoot({
  ...rootReducer().reducers
}),

It works in development mode but it doesn't in production. Why?

Appreciate if someone could help. Thanks in advance.

like image 305
KnowledgeSeeker Avatar asked Oct 18 '18 03:10

KnowledgeSeeker


People also ask

Are reducers invoked inside components directly?

Yes - all the reducers will get called when you dispatch the action.

What is NgRx store dispatch?

They listen for actions dispatched from ngrx/store, meaning that we can dispatch something like a LOAD Action and listen to this action into an effect executing a particular code. They isolate side effects from components, allowing for more pure components that select state and dispatch actions.

Is NgRx store persistent?

Currently ngrx/store doesn't support such functionality. But you can maintain state by using a library like ngrx-store-persist to save data to the browsers indexedDB, localStorage or WebStorage. This library automatically saves and restores ngrx/store's data.


1 Answers

I manage to solve the problem by changing the way of how I implemented my rootReducer and rootActions. Instead of exporting a function I returned declared 2 seperated objects.

Here is my old code below of my rootReducer and actions

export default function () {
  return {
    reducers: {
      newLocation: NewLocationReducer,
      newOrder: NewOrderReducer,
      orderTaken: OrderTakenReducer,
      driverUpdateProfile: DriverUpdateProfileReducer,
      driverUpdateAvatar: UpdateAvatarReducer,
      newTransaction: NewTransactionReducer,
      updateTransaction: UpdateTransactionReducer,
      orderNewMessage: OrderNewMessageReducer,
    },
    actions: {
      newLocation: NewLocation,
      newOrder: NewOrder,
      orderTaken: OrderTaken,
      driverUpdateProfile: DriverUpdateProfile,
      driverUpdateAvatar: UpdateAvatar,
      newTransaction: NewTransaction,
      updateTransaction: UpdateTransaction,
      orderNewMessage: OrderNewMessage,
    }
  }
}

and this is the new code below:

export const rootActions = {
  newLocation: NewLocation,
  newOrder: NewOrder,
  orderTaken: OrderTaken,
  driverUpdateProfile: DriverUpdateProfile,
  driverUpdateAvatar: UpdateAvatar,
  newTransaction: NewTransaction,
  updateTransaction: UpdateTransaction,
  orderNewMessage: OrderNewMessage,
}

export const rootReducer = {
  newLocation: NewLocationReducer,
  newOrder: NewOrderReducer,
  orderTaken: OrderTakenReducer,
  driverUpdateProfile: DriverUpdateProfileReducer,
  driverUpdateAvatar: UpdateAvatarReducer,
  newTransaction: NewTransactionReducer,
  updateTransaction: UpdateTransactionReducer,
  orderNewMessage: OrderNewMessageReducer,
}

I declared them into two seperated variables which I think it is more organized instead of the old one.

like image 140
KnowledgeSeeker Avatar answered Nov 15 '22 03:11

KnowledgeSeeker