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

339
Visualizações
NextJS Dynamic List Not Updating on Delete/Update

Currently, my react/nextjs dynamic list is not updating correctly, I've given an array to map so it'll show a new row on frame update since it's stored on useState, Usually, the issue is with the key of the list that is not unique, but my key is unique

Heres my code

const [allSelectedMaterials, setAllSelectedMaterials] = useState([]) // This variable stores a javascript object into the array
{console.log('-- Updated --')}
{allSelectedMaterials.map((material, i) => {
    const key = Object.keys(material).toString()
    console.log(`${i} - [${key}] ${material}`)

    return (
        <div key={key}>
            <Row className='align-items-center'>
                <Col xs='auto'>
                    <h6>{material[key].name}</h6>
                </Col>
                <Col>
                    <Button variant='danger' className='mb-1' onClick={() => handleDeleteMaterial(key)}>
                        Remove
                    </Button>
                </Col>
            </Row>
            <InputGroup>
                <InputGroup.Text>
                    <Image src={material[key].image} className={`${styles.allMaterialsImage}`} />
                </InputGroup.Text>
                <Form.Control type='number' min={1} ref={(e) => (selectedMaterials.current[i] = e)} required />
            </InputGroup>
            <div className='mt-1' />
        </div>
    )
})}

The problem is after I added the first item on the list and adding a new one it won't update the view, here's the console output

enter image description here

And here's me adding a second entry to the list

enter image description here

It clearly says on the log that the array (stored in useState) is updated but it's not changing the view it's still the same as the previous one. But if I reupdate the frame by updating any useState variable it updated the list

Update: Here's my code for adding new material (loadedMaterials is just a list of materials that is retrieved from an API)

const handleAddSelectedMaterial = () => {
    loadedMaterials.map((material) => {
        const key = Object.keys(material)
        if (key == currentSelectedMaterial) {
            let _material
            if (allSelectedMaterials.length > 0) _material = allSelectedMaterials
            else _material = []

            _material.push({ [material[key].id]: material[key] })
            setAllSelectedMaterials(_material)
        }
    })
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try replacing from

 _material.push({ [material[key].id]: material[key] })
            setAllSelectedMaterials(_material)

to

 _material.push({ [material[key].id]: material[key] })
            setAllSelectedMaterials([..._material])
about 4 years ago · Juan Pablo Isaza Relatório

0

Thank you everyone for the input and responses, but I've managed to solve this issue by redoing how the form & dynamic list work and it's working perfectly now.

If anyone is wondering I just basically follow this person implementation on a dynamic list https://www.cluemediator.com/add-or-remove-input-fields-dynamically-with-reactjs

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