I have a Redux Reducer that needs to store the previously selected option each time a user selects a new option.
.
export default function reducer(state = [], action) {
switch (action.type) {
case previousSelectionAction.PREVIOUS_SELECTION: {
// Need to return the previous selection
// Have tried:
// return [...state] and other variations with action.payload and dropping item in array
// but this just returns the same state, does not update each tie a user selects a new option
}
default:
return state;
} }
Store the previous value in a different state property when the selection changes:
case updateSelection:
return {
...state,
previous: state.current,
current: action.payload
}
And then you can select that previous value.
Instead of making your state a single object, it can be 2 objects one storing the previous value and one storing the current value.
The logic will be something like the following:
initialState = { previous: {}, current: {}}
Case updateSelection:
return {
...state,
previous: state.current,
current: action.payload
}
Then, you could check state.previous in your code.