I have a property in my state which is Map<string, object | 'error'> . When I get response from service I wish to update this map. I have tried below in the reducer
on(setMapData, (state, action) => {
const { data } = action
data.forEach( (value, id) => {
let dataMap = state.dataMap
dataMap.set(id, value)
return {
...state,
dataMap: {...state.dataMap, ...dataMap}
}
})
//Till here I can see dataMap having all the values in state.
return {
...state
//dataMap: state.dataMap
}
// But nothing is returned from here, my selectors are not getting invoked.
})
Below is my state object
export const initialState: dataState = {
loading: true,
dataMap: new Map<string, object | 'error'>()
}
Please help me in this issue. Thanks in advance.
I can think of two solutions :
ImmerorImmutableJsto generate a copy of Map at the reducer and then update the values. for example usingimmeryour code code be something like this: