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.
Yes - all the reducers will get called when you dispatch the action.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With