Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda