Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!