Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
Redux reducer return previous state dynamically

I have a Redux Reducer that needs to store the previously selected option each time a user selects a new option.

  • User Selects A (returns empty array)
  • User Selects B (returns A)
  • User Selects C (returns B)
  • User Selects D (returns C)

.

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;

} }

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda