I am coding along with the Redux Essentials tutorial.
When I run notificationArray.map without using the return value, my state is updated per the map functions callbacks.
I.e. in allNotifiicationsRead
, each notifications's read
property is set to true
in the app state. And in fetchNotifications
, all each notification's isNew
property is set to !read
.
I would expect this notificationArray.map(...)
call to not have any effect since, I am not using the return value, let alone editing the state with it such as something like notificationsAdapter.setMany(notificationArray)
.
Any clarification on how this is working would be appreciated.
Please let me know if I didn't include any relevant details about this app.
const notificationsSlice = createSlice({
name: 'notifications',
initialState: notificationsAdapter.getInitialState(),
reducers: {
allNotificationsRead(state, action) {
const notificationArray = Object.values(state.entities)
notificationArray.map(notification => (
notification.read = true
))
}
},
extraReducers(builder){
builder.addCase(fetchNotifications.fulfilled, (state, action) => {
notificationsAdapter.upsertMany(state, action.payload)
const notificationArray = Object.values(state.entities)
notificationArray.map(notification => (
notification.isNew = !notification.read
))
})
}
})