Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140734

How to find the updated state within a redux reducer?

$
0
0

I am trying to set up the ability to cancel notifications for users. When a notification is scheduled I get a notification ID that I store along with data to uniquely identify the object. When I try to find the notification ID I am experiencing problems. The state.notificationIDs is always the initial state that the session began with. When I add a notificationID I see it in the redux store, but state.notificationIDs can't access it until I reload and initial state changes.

When I run it in the same session my notificationData comes back undefined, and if I reload it and then try canceling, notificationData comes back with the correct object.

How can I change it so I grab the updated notification IDs? I know I could grab it in the action, however, I want to delete it from notificationIDs after I cancel it so I need to find the updated state in the reducer.

Here is my reducer where I cancel the notifications:

const initialState = {
  notificationIDs: []
};

export default (state = initialState, action) => {
  switch (action.type) {
    case PERSIST_REHYDRATE:
      return action.payload.RemindersReducer || [];
    case ADD_NOTIFICATION_ID:
      return {
        ...state,
        notificationIDs: [...state.notificationIDs,
          {
            itemID: action.item.id,
            reminderType: action.reminderType,
            notificationID: action.notificationID
          }]
      };
    case CANCEL_NOTIFICATION: { //works perfectly if different session, same session doesn't work
      const notificationData = state.notificationIDs.find(
        obj => obj.itemID === action.id && obj.reminderType === action.reminderType
      );
      console.log(notificationData);
      Notifications.cancelScheduledNotificationAsync(notificationData.notificationID);
      return state;
    }
    default:
      return state;
  }
};

Viewing all articles
Browse latest Browse all 140734

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>