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

221
Vistas
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 Respuestas
Responde la pregunta

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 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