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

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

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 Denunciar

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