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

218
Views
React setState, using array methods in callback function

I am currently following a React course on Scrimba on creating a web app for taking notes.

The problem requires me to bump a note to the top of the note list every time it's updated.

The notes are initialised through useState as follows:

const [notes, setNotes] = useState([])

The array consists of the individual notes as objects with an id and body

Every time an onChange is triggered, the following function is ran:

function updateNote(text) {
        setNotes(oldNotes => {
            let updated = oldNotes.map(oldNote => {
            return oldNote.id === currentNoteId
                ? { ...oldNote, body: text }
                : oldNote
            })
            const currNoteIndex = updated.findIndex(
                note => note.id === currentNoteId
                )
            console.log(currNoteIndex)
            updated.unshift(updated.splice(currNoteIndex, 1))
            return updated
        })
    }

However, I keep getting an error as shown in the image.

Error Message

It's very unclear to me where the problem lies, but I'm thinking it has to do with the array methods.

Any explanation for this issue would be greatly appreciated!

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

0

Credits to jsN00b for the answer:

array.splice returns an array, not the object.

Since that array is inserted to the start of the array containing the objects, there will be an error when updateNote() is called again.

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!