Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

0

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

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

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda