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

204
Visualizações
React Cannot read properties of undefined (reading, 'setState') on trigger from passed prop

Im working on learning react with a project I thought up.

I have a parent component that has a handleClick function:

handleEmail() {
     console.log('handle email triggered');
    this.setState({
        viewDisabled: 'hide',
        
    })
 }

when I trigger it from within the same component, like

<button onClick={this.handleEmail}>handle email</button>

it logs "handle email triggered" and properly sets the state.

I have a child component with a form:

<form onSubmit={this.handleSubmit}>
            <legend>Enter a valid email to start playing!</legend>
      <input type="email" value={this.state.value} onChange={this.handleChange} />

            <input type="submit" value="Submit" onClick={this.props.handleEmail} />
        </form>

called in the parent component like:

  <Email handleEmail={this.handleEmail} onChange={emailHandler} />

When the submit button is clicked, it bubbles up, and the console logs "handle email triggered", but it errors with "Cannot read properties of undefined (reading, 'setState')" on attempting to set the same state that the native click can set. I would think I was doing something wrong, but it follows the same pattern as another child component that works using the same method and I can't tell the difference. Any idea what this means would be helpful. I'm guessing it has something to do with state not properly getting set somewhere along the way. Thanks in advance.

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

0

This is an issue with this binding. You can use an arrow function to avoid it. Arrow functions has lexical binding of this.

handleEmail = () => {
     console.log('handle email triggered');
    this.setState({
        viewDisabled: 'hide',
        
    })
 }

Or you can do this binding to the handleEmail inside the constructor of the class component like below.

constructor(props) {
   super(props);
   this.handleEmail = this.handleEmail.bind(this);
}
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