Tengo un Redux Reducer que necesita almacenar la opción previamente seleccionada cada vez que un usuario selecciona una nueva opción.
.
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;} }
Almacene el valor anterior en una propiedad de estado diferente cuando cambie la selección:
case updateSelection: return { ...state, previous: state.current, current: action.payload }Y luego puede seleccionar ese valor anterior.
En lugar de hacer que su estado sea un solo objeto, pueden ser 2 objetos, uno que almacene el valor anterior y otro que almacene el valor actual.
La lógica será algo como lo siguiente:
initialState = { previous: {}, current: {}} Case updateSelection: return { ...state, previous: state.current, current: action.payload }Luego, podría verificar state.previous en su código.