0

I have an array of Notifications as a prop for my NotificationCenter component.

When an user clicks on any notification (or on the "Mark All As Read" button), they should be marked as 'isRead = true' and then re-render it with another color on the UI.

I already did everything (Actions, Events, etc.) but now I need to trigger the Reducer so my Notifications prop can be re-rendered.

This is my code so far:

if (isType(action, ActionTypes.MarkNotificationsAsRead)) {
    return Object.assign({}, state, { 
        notifications: state.notifications.map(n => action.payload.<<HELP>>)
    } as INotificationCentreState);

"state.notifications" is my Notification prop with all the available notifications.

"action.payload" is the string array containing the IDs of the notifications that I should match and then modify to 'notification.isRead = true'.

The main issue that I'm having here is that I can't mutate my notification list and I'm not completely sure how .map() works.

Can anyone give me a hint? I hope my problem is clear.

Thanks!

1 Answer 1

1

You could toggle the isRead depending on whether id of current notification is in action.payload

state.notifications.map(n => Object.assign({}, n, {isRead: action.payload.indexOf(n.id) > -1}))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! I just needed to add a condition at the end because it was toggling the IsRead property, but it worked as a charm! IsRead: action.payload.indexOf(n.Id) > -1 ? true : n.IsRead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.