I am trying to modify my array every time with new values but not able to achieve that functionality.
So the code for pushing array elements is given like this:
const updateYears = (state, action) => {
state.dropDownData.years = [...new Set([action.payload])];
}
And my initial state is like this:
const initialState = {
dropDownData: {
programs: [],
years: [],
},
};
The above reducer function is pushing elements inside "years" array.
locationSummaries.map((locationSummary)=>dispatch(updateYears(locationSummary.year)));
locationSummaries is the array of objects containing year as one of the properties.
So when my year array contains the values [2017, 2018, 2019] and when I am pushing [2018, 2019] inside this, I am still having [2017, 2018, 2019] inside the original year array. Instead what I want is modified year array to [2018, 2019]. I believe it is happening because same values are getting pushed inside the array.
So how should I modify my reducer function to achieve this modification.
If action.payload is array and if you want to modify the array completely, why don't you just assign new array like this:
const updateYears = (state, action) => {
state.dropDownData.years = action.payload;
}