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

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

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