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

175
Visualizações
Getting error when trying to invoke child method in react.js : Uncaught TypeError: Cannot set properties of undefined (setting 'child')

I am trying to call child component method , I tried various solution from internet but nothing worked. Here is the stackblitz link

Here is the code snippet

Parent.component

import React, { Component } from 'react';
import Child from './child';

const Form = (props) => {
  return (
    <div>
      <Child
        ref={(instance) => {
          this.child = instance;
        }}
      />
      <button
        onClick={() => {
          props.callparent(this.child);
        }}
      >
        Click
      </button>
    </div>
  );
};

class Parent extends Component {
  parentfunc = (childref) => {
    //invoking child method
    childref.getAlert();
  };
  render() {
    return (
      <div>
        <Form
          callparent={() => {
            this.parentfunc();
          }}
        />
      </div>
    );
  }
}

export default Parent;

child.js

import React, { Component } from 'react';

class Child extends Component {
  getAlert() {
    alert('clicked');
  }

  render() {
    return <h1>Hello</h1>;
  }
}

export default Child;

When I try to invoke child method childref.getAlert(); I see Uncaught TypeError: Cannot set properties of undefined (setting 'child').

I am sure I am not creating the child reference correctly and passing it to the function to invoke child method. Can someone please help me here!

about 4 years ago · Juan Pablo Isaza
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