I have one value from the server. It contains object with the same key as in my state object. Key value of that object is an array with one item. To handle this value in my reducer I need to add this array item to existing object key in my state.
In short:
I have from the server: { TEST: ['item1'] }
My state is: { TEST: ['item2'], TEST2: ['item3'] }
Expected result: { TEST: ['item1', 'item2'], TEST2: ['item3'] }
const handleCreateItemFilter = (state, { payload }) => {
const { list } = payload;
const updatedList = { ...state.regions.list };
updatedList[Object.keys(list).join()] = [...updatedList[Object.keys(list).join()], Object.values(list).join()];
return {
...state,
regions: {
...state.regions,
list: updatedList,
},
selectedFilter: {
...state.selectedFilter, unassignedCountries: [], regions: Object.keys(list).join(),
},
};
};
Currently, I solved it like in the example above: create new updatedList, find appropriate key for my object from the server and assign to my new object array of old key value and new one. But I think it isn't good approach to solve this problem.