I have an eventEmitter that fires isOnline true or false for when server communications drop. I added 1 if statement to prevent the dispatch firing if the onLine state matches the request to go onLine or offLine. Removing this if fixes the code, but I am stuck with dispatch calls for no reason.
Keep in not this has no issues going from onLine to offLine. It just has issues going from offLine back to onLine.
const setIsOnline = (isOnline: boolean) => {
if (state.isOnline !== isOnline) {
console.log('Online status changed from', state.isOnline, 'to', isOnline);
dispatch({
type: navigatorActions.SET_IS_ONLINE,
payload: isOnline
});
}
};
Here is the reducer for reference. Note the reducer works fine when called. The issue is that if statement prevents it from being called when going online from offline. Taking out the if statement fixes it all, but now I have redundant rerenders.
import {
navigatorActions,
NavigatorState
} from '@shared/utils/domains/context/types';
export const navigatorReducer = (state: NavigatorState, action: any) => {
let updatedState = state;
switch (action.type) {
case navigatorActions.SET_BACK_PATH:
updatedState = { ...state, backPath: action.payload };
break;
case navigatorActions.SET_LOGO_CLICKED:
updatedState = { ...state, logoClicked: action.payload };
break;
case navigatorActions.SET_IS_ONLINE:
console.log('reducer changing', state.isOnline, 'to', action.payload);
updatedState = { ...state, isOnline: action.payload };
break;
default:
return state;
}
return updatedState;
};