Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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 answers
Answer question

0

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

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

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!