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

182
Visualizações
React components can not get the right argument when passing closure

I'm using react to write a page to show pictures in browser. I'm using the following code to render a picture-show-block

renderPicShowBlock= () => {
    if (this.state.records)
    {
        let picBlocks = []

        for (const record of this.state.records) {
            picBlocks.push(
                <PicShowBlock user={this.state.user}
                              key={record.id}
                              record={record}
                              onItemDelete={()=>this.handleItemDelete(record.id)}
                />
            )
        }
        return picBlocks
    } else {
        return null
    }
}

handleItemDelete is defined as an arrow function:

handleItemDelete= (id) => {
    axios.post("/core/data/record/",
        {"action": "delete", "item_id": id}).then((response)=>{
        console.log(response.data)
        this.init_record()
    })

}

The problem is when I call onItemDelete in this component, the record in this closure get stuck pointing to the last record in this.state.records. I was thinking that it might be that "JS closure trap" caused by JS glocal scpoe, but it seems not. What does this problem come from? Is that caused by closure or some code logic bugs?

I used create-react-app to setup this project.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can turn it around:

                <PicShowBlock user={this.state.user}
                              key={record.id}
                              record={record}
                              onItemDelete={this.handleItemDelete(record.id)}
                />

and

handleItemDelete= (id) => () => {
    axios.post("/core/data/record/",
        {"action": "delete", "item_id": id}).then((response)=>{
        console.log(response.data)
        this.init_record()
    })

}

This way, this.handleItemDelete(record.id) will be executed immediately with the right record and the function just returns the callback you want on your component.

about 4 years ago · Juan Pablo Isaza Relatório

0

Oh, I found that it was caused by another issue. It is because that the logic to process deletebutton's id is wrong, The Bootstrap's javascript pick another delete button from anothor ShowBlock

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