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

130
Visualizações
Pass a function from a Functional Component to a Class based Component

Parent Component is like below

import React, { useState } from "react";
import EntityDefinition from "./EntityDefinition";

export default function EntitySelection(props) {

  const testFun = () => {
    console.log("Function activated");
  };
  return (
    <>
        <div>
                <EntityDefinition                  
                  testFun={testFun}
                />{/**Calling a Class based Component*/}
        </div>
    </>
  );
}

Class based Component (Child)

import React from "react";
import { ComboBox, DropdownOption, Button } from "react-widgets";
import axios from "axios";

export default class EntityDefinition extends React.Component {

  render() {
    return (
      <React.Fragment>       
        <div>
          {" "}
          <Button onClick={this.testFun}>Close</Button>{" "} {/*/Calling the function passed*/}
        </div>
      </React.Fragment>
    );
  }
}

but when i clicked the button the testFun function is not being called. i tried something like onClick={this.state.testFun} , onClick={testFun} but nothing is happening. can someone point what am i doing wrong here.

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

0

testFun is a prop. So use this.props.testFun

onClick={this.props.testFun}
about 4 years ago · Juan Pablo Isaza Relatório

0

testFun is a prop, and you are using the ES6 class component, and it receives the props as a props object, so you can try accessing it as below

onClick={this.props.testFun}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to refer to props passed to a class based components using this.props from inside the class based components: Docs

In your case, you should change the onClick listener to this:

<Button onClick={this.props.testFun}>
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