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

74
Views
Reverse state in redux

I'm trying to implement sorting functionality in my notes app but have some problems with it. My sort function must just reverse an array of notes but it does not work.

I have a state with notes:

    export const initialState = {
      notes: [
        {
          id: nanoid(),
          text: '',
          date: '',
        },
      ],
    }

Action:

    export const sortNoteAction = ([notes]) => ({
      type: SORT_NOTE,
      payload: [notes],
    })

Reducer:

    export default function notes(state = initialState, { type, payload }) {
      switch (type) {
        case SORT_NOTE: {
          return {
            ...state,
            notes: [payload].reverse(),
          }
        }
        default:
          return state
      }
    }

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Action:

    export const sortNoteAction = (notes=[]) => ({
      type: SORT_NOTE,
      payload: notes,
    })


Reducer:

    export default function notes(state = initialState, action) {
      switch (action.type) {
        case SORT_NOTE: {
          return {
            ...state,
            notes: action.payload.reverse(),
          }
        }
        default:
          return state
      }
    }
about 4 years ago · Juan Pablo Isaza Report

0

I think it doesn't work because you are essentially sorting a list with just one item. If you change [notes] to notes (2 occurrences) and [payload] to payload (1 occurrence) it will probably work

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!