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

167
Visualizações
Send value to parent function when class renders a child component

I have a regular class file in react rendering a child component.

class Parent extends React.Component{
  constructor(props){...}

  onBtnClicked = ({index}) => {
    this.setState({ btnIndex:index })
  };


  render(){
    return(
      <Child onBtnClicked={this.onBtnClicked} />
    )
  }
}

In the child, I'm calling a onBtnClicked function and sending a value.

export default Child = (props) => {
  let prop = {...props};
  return(
    <Button onClick={handleDragEnd}> </Button>
  )

  function handleDragEnd() {
    prop.onBtnClicked('87612876');
  }
}

I see that the parent onBtnClicked function is called. But the value is undefined.

How could I pass the value?

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

0

You are passing a string to prop.onBtnClicked

prop.onBtnClicked('87612876');

but attempting to destructure an index property, which from a string, is undefined.

onBtnClicked = ({ index }) => { // <-- "87612876".index -> undefined!
  this.setState({ btnIndex: index })
};

Either don't destructure in the parent callback:

onBtnClicked = (btnIndex) => {
  this.setState({ btnIndex });
};

or pass the value from the child component in an object with index key so the parent's callback can destructure it:

function handleDragEnd() {
  prop.onBtnClicked({ index: '87612876' });
}
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