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

147
Visualizações
How to trigger parent componentDidUpdate by click in child component

In my parent component, I have a class component called ListOfItems.js, that lists a bunch of objects like so:

class ListOfItems extends Component {
  construction (props) {
    super(props);
    this.state = {
      objectList: []
    }
  }
}

// This gets the list of objects to display
componentDidMount() {
   this.getObjectList();
}

componentDidUpdate(_prevProps, prevState) {

  if( this.state.ObjectList !== prevState.ObjectList) {

     // this is called infinitely which is a problem
     console.log("entered");
     this.getObjectList();
  }
}

getObjectList = () => {
  const input = { id_for_this_list: id }

  fetch( connectToApi, {
     method: "PUT",
     headers: { //stuff}
     body: JSON.stringify(input)
  })
     .then(res => //)
     .then(result => 
        if( result.length >= 0) {
            this.setState( {...this.state, objectList: result });
        }   
     )
     .catch(err=>{console.error(err)});
}

render () {
  return (
    {this.state.objectList.map(item) => {
      <Object 
        objectList={this.objectList}
        data={item}
       //few other props here
      >
    }}
  )
}
     

In my child component, I have a functional component called Object.js. In this, I have a delete functionality, where if the user selected the "x" icon on the object, that object is deleted from the list.

// 'x' image that, when clicked on, deletes object from list of objects
// (heavily simplified for stackOverFlow)

<img onClick= {() => { deleteObject() })>

const deleteObject() = () => {
   fetch( connectToApi, {
     method: "DELETE"
   })
      .then(res => //)
      .then(result => //deletion made and confirmed succesful)
      .catch(err => console.error(err))
}

How do I trigger the componentDidUpdate() in ListOfItems.js (parent), when I click on the "x" on an Object (child)?

Btw, the parent must be a class component and the child must be a functional component, unfortunately.

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

0

You never appear to set the state for your parent component when you call getObjectList so that could be an issue for you, I'm not sure if you're left it out of this excerpt on purpose.

Either way, you'll need to pass down a function from the parent component that sets the state held in the parent component. That will cause the parent to rerender.

If you don't want to change the state you can potentially call a function that you pass from the parent that forces a refresh, but this wouldn't be ideal.

parentResetFunction = () => {
   this.forceUpdate()
}
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