Andres Elizondo Andres Elizondo - 1 year ago 113
TypeScript Question

TypeScript Redux React: How to iterate through a component children

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: => 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.


Answer Source

You could toggle the isRead depending on whether id of current notification is in action.payload => Object.assign({}, n, {isRead: action.payload.indexOf( > -1}))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download